I need to compare two DateTime
values in my DotNetNuke module, like this:
// This value actually comes from the database.
DateTime time1 = Convert.ToDateTime("6/2/2013 5:21:05 PM");
// Say it is now "5/31/2013 2:20:33 AM"
DateTime now = DateTime.Now;
int num = DateTime.Compare(time1, now);
As you can see, num == 1
because the DateTime
value of time1
is greater than the DateTime
value of now
. But when I change the language to German (de-DE
) in the DotNetNuke settings, the value of now
becomes - 31/5/2013 2:20:33 AM
. The result is that num
becomes -1
, meaning time1
is earlier than now
. This is incorrect because time1
is actually later than now
.
The time1
value is always in mm/dd/yyyy
format because it is coming from the server.
So, how do I get the right comparison in case another language is set in DotNetNuke? When I searched the web, I found that I can correct the time with the following code:
provider = new CultureInfo("en-US");
DateTime.ParseExact("5/31/2013", "d", provider);
Is that correct? Is there a better way to handle this concern?
Edited
forgot to say that time 1 comes from my website server before saving to db and is compared with client's website's datetime value in now variable.all codes is my app which is in that client site.these codes at first accessed time1 date by .net methods of getting remote site data.so when that dnn site language is set as de-DE look 'now' variable value is day first which is reverse from my site date is US format month first.in consequence when comparison takes place, my server date is resulted as earlier than client site date! And everything breaks.FYI my server date came as a i week later date which is 2nd june for license purpose.
Edited again:
Hi every1.I found a solution by the following codes.This date comes from my another server as i said above - "6/15/2013"(changing it was bef4 6/2/2013).It's in string format, why it's a string? it's a long history.just let it be a string right now.before converting to string it was in US date format(month before).Now in current website where my app is, these codes seem working.Hope i am not wrong, please confirm
CultureInfo provider = CultureInfo.InvariantCulture;
provider = new CultureInfo("en-US");
DateTime time1 = DateTime.ParseExact("6/15/2013", "d", provider);
DateTime now = DateTime.Now;
int num = DateTime.Compare(time1, now);
"num" returning 1 which is right whether dnn site language is German or English.