theinarasu theinarasu - 2 months ago 15
ASP.NET (C#) Question

different between date format when get current date only using datetime c#

if my computer datetime format as yy/mm/dd or mm/dd/yyyy or regardless formation

Datetime date1 = ... /// assume date1 is date1 < date2 and date1 < date3
DateTime date2 = DateTime.Parse(DateTime.Now.ToString("dd-MM-yyyy")) ;
DateTime date3 = DateTime.Now.Date;

DateTime.Compare(date1 ,date2); and DateTime.Compare(date1 ,date3);


what would the result ? is it for
date2
and
date3
return 1 when compare
date1
?
DateTime.Parse(DateTime.Now.ToString("dd-MM-yyyy")
is equal as
DateTime.Now.Date
?

Bob Bob
Answer

Not sure why are you doing DateTime.Parse(DateTime.Now.ToString("dd-MM-yyyy"));..?

To answer your question, both date2 and date3 will become different depending on your computer (or the device that is running this code) localization.

So if date1 is always earlier you can do TimeSpan diff = date3 - date1; which will give you TimeSpan object. Then you can diff.Days to get the number of days difference.

And yes date3 will only have date in there. The time will be set to 12:00 midnight. See here.

TimeSpan.Days

Comments