Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hello,
I need to compare dates in winforms, using vb.net
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?
 
Thanks
Posted 2-Feb-13 20:55pm
Rate this: bad
good
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.
 
Ex:
String someDateS = currDate.ToString(CultureInfo.InvariantCulture);
String otherDateS = DateTime.Parse(otherDate, CultureInfo.InvariantCulture); 
// Compare now
if(someDateS.Equals(otherDateS))
{
}
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,609
1 Sergey Alexandrovich Kryukov 6,240
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
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