0

in my wpf app, i am using datepicker control to select dates which is shown below:

<DatePicker Name="dtpSales" Grid.Row="3" SelectedDate="{Binding SalesDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue ={x:Static System:DateTime.Now}}" Width="200" Height="24" HorizontalAlignment="Left" Margin="147,7,0,5" BorderBrush="Black" Grid.ColumnSpan="2" />

And I am able to save the date in mm-dd-yyyy format. How do i save it in 25-05-2019 format ??? The binded SalesDate is a DateTime datatype.

EDITED : I have a date object with value 12-02-2019 (dd-MM-yyyy) and it is saved to sql server table as 2019-12-02 (yyyy-dd-MM). When i retrieve the date from the database, the value of the date will be 02-12-2019 (MM-dd-yyyy). And Now I want to add 4 months to that date.

        int salesid = (int)dr["SalesID"]; // dr is the datarow
        DateTime salesdate = (DateTime)dr["SalesDate"];
        string sdate = salesdate.AddMonths(4).ToString("dd-MM-yyyy");
        DateTime servicedate = Convert.ToDateTime(sdate); // 2020-12-02

When I am adding 4 months to 02-12-2019, it will become 2020-12-02 ??? I am not able to get the correct date when i am adding just 4 months to 02-12-2019.

Kuttan
  • 107
  • 4

1 Answers1

0

EDITED: It appears from the comments that your database is already returning a DateTime object. The below should be enough:

int salesid = (int)dr["SalesID"]; // dr is the datarow
DateTime salesdate = (DateTime)dr["SalesDate"];
DateTime servicedate = salesdate.AddMonths(4); // 2020-12-02

(Thank you Chris Dunaway for suggesting improvements)

Jacob JA Shanks
  • 370
  • 1
  • 13
  • error throws at this line DateTime salesdate = Convert.ToDateTime(DateTime.ParseExact(dr["SalesDate"].ToString(), "MM-dd-yyyy", CultureInfo.InvariantCulture)); The error ssays "string was not recognized as a valid DateTime" – Kuttan Feb 12 '19 at 16:22
  • What exactly is your database returning? I have tested this with the following string: `"02-12-2019"` and this works – Jacob JA Shanks Feb 12 '19 at 16:25
  • the database returns 02-12-2019. But the line DateTime salesdate = Convert.ToDateTime(DateTime.ParseExact(dr["SalesDate"], "MM-dd-yyyy", CultureInfo.InvariantCulture)); throws error and it says the datarow has invalid dataformat. So I added .ToString() to dr["SalesDate"]. Again it threw exception. – Kuttan Feb 12 '19 at 16:32
  • 1
    Converting a `DateTime` to a `string` and then back to a `DateTime` makes no sense. Why not just do `DateTime servicedate = salesdate.AddMonths(4);`? – Chris Dunaway Feb 12 '19 at 16:36
  • If what your saying is `dr["SalesDate"]` is equal to `"02-12-2019"` in the debugger when it hits that line then I'm not sure where the issue is Try pasting this line where the date is passed directly as a string and run it just to ensure your debugger is working correctly: `DateTime salesdate = Convert.ToDateTime(DateTime.ParseExact("02-12-2019", "MM-dd-yyyy", CultureInfo.InvariantCulture));` – Jacob JA Shanks Feb 12 '19 at 16:36
  • @Chris Dunaway: you could do this but the issue is with Line 2 in actually reading the date in correctly from the database – Jacob JA Shanks Feb 12 '19 at 16:38
  • `DateTime` _has no format_. Your conversion to `string` and then back to `DateTime` accomplishes exactly nothing. It's an extra step that is not needed. Also calling `Convert.ToDateTime` on the result of `DateTime.ParseExact` makes no sense either. `ParseExact` returns a `DateTime` – Chris Dunaway Feb 12 '19 at 16:39
  • @ChrisDunaway: I agree with you on this but making this change to the answer right now isn't solving the OP's immediate problem – Jacob JA Shanks Feb 12 '19 at 16:41
  • The OP needs to clarify what the data type in `dr["SalesDate"]` is. – Chris Dunaway Feb 12 '19 at 16:46
  • Again it throws exception "string was not recognized as a valid DateTime" when reaching the the second line DateTime salesdate1 = (DateTime)dr["SalesDate"]; DateTime salesdate = Convert.ToDateTime(DateTime.ParseExact(salesdate1.ToString(), "MM-dd-yyyy", CultureInfo.InvariantCulture)); – Kuttan Feb 12 '19 at 16:46
  • Try the edited answer, maybe that will help. Could you show what exactly `dr["SalesDate"]` contains? – Jacob JA Shanks Feb 12 '19 at 16:48
  • @Kuttan: Your saying it failed on the second line? I believe your database is returning a DateTime object already. Can you confirm if `salesdate1` has the correct date already in it before reaching line 2? – Jacob JA Shanks Feb 12 '19 at 16:52
  • dr["SalesDate"] contains 2019-12-02 00:00:00.000 in the DB. its a datetime type.Yes it contains the value before reaching the 2nd line. – Kuttan Feb 12 '19 at 16:54
  • @Kuttan in that case try just importing it to a DateTime like you have done already and adding 4 months, remove all conversions to string as they are unnecessary. I'll update the answer – Jacob JA Shanks Feb 12 '19 at 16:55
  • @JacobJAShanks boss i used ur updated code. it will not work. because the second line DateTime salesdate = dr["SalesDate"] needs a data conversion. dr["SalesDate"] always returns an "object" type. so it needs to converted to datetime type. if so, it will not work. any other idea ??? – Kuttan Feb 12 '19 at 17:09
  • @Kuttan I'll add the data converter, Looks like I missed that out – Jacob JA Shanks Feb 12 '19 at 17:10
  • @JacobJAShanks hi boss, i have already used the same datetime converter. it will not work. the code DateTime salesdate = (DateTime)dr["SalesDate"] returns 02-12-2019 (dd-MM-yyyy) format. when we add 4 months to that date, it becomes 02-04-2020 (dd-MM-yyyy) format. this is wrong.Here we are expecting 12th Feb 2019. – Kuttan Feb 12 '19 at 17:21
  • That's correct though? Your SQL stores `2019-12-02 00:00:00.000`. SQL formats DateTime Objects like this: `yyyy-MM-dd HH:mm:ss.fff`. So the Date in your Database is the 2nd December 2019, 4 months laters makes it 2nd April 2020, aka 02-04-2020. Your code is correct, I think you just need to tweak the data in your database – Jacob JA Shanks Feb 12 '19 at 17:24
  • @Kuttan: please refer [here](https://www.w3schools.com/sql/sql_dates.asp) for the DateTime formatting in SQL – Jacob JA Shanks Feb 12 '19 at 17:41