Click here to Skip to main content
13,047,502 members (61,331 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I need to compare dates in winforms, using
I am trying using
If Now() >= DateTime.Parse("17.01.2013 08:47:10 PM")

The problem is my date is in dd/mm/yyyy format but when this application runs on a system where the format is mm/dd/yy, it gives problem.

How can I bring the system date (obtained using NOW()) in dd/mm/yyyy format so it could be compared with my given date?

Posted 2-Feb-13 20:55pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should not rely on the local settings of the server instead you should be deciding what format you want.

One of the ways is to use: MSDN: Using the InvariantCulture Property[^] - The InvariantCulture property represents neither a neutral nor a specific culture. It represents a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.

String someDateS = currDate.ToString(CultureInfo.InvariantCulture);
String otherDateS = DateTime.Parse(otherDate, CultureInfo.InvariantCulture); 
// Compare now
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Although you can compare dates as string, it is not wise. "Now" return a datetime structure, that is comparable as it should be. The approach you have taken in your code row pasted here is still good, but you have to look in opposite direction: make DateTime.Parse process your date format. You can do that, since the Parse method has an overload[^] that takes an IFormatProvider. If this format is specific to a culture, pass a culture:
DateTime.Parse(dateString, CultureInfo.CreateSpecificCulture("fr-FR"));

If not, make you own provider, or use ParseExact[^], by giving a proper format string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100