Click here to Skip to main content
15,943,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I need to compare dates in winforms, using vb.net
I am trying using
VB
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?

Thanks
Posted

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.

Ex:
C#
String someDateS = currDate.ToString(CultureInfo.InvariantCulture);
String otherDateS = DateTime.Parse(otherDate, CultureInfo.InvariantCulture); 
// Compare now
if(someDateS.Equals(otherDateS))
{
}
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900