?
Solved

How to compare 2 DateTime objects in different locale?

Posted on 2013-01-09
15
Medium Priority
?
275 Views
Last Modified: 2013-01-17
I have 2 dates that need to be compared... works fine in the US, but not in the UK, where month comes after the day (Jan 5th is 5/1/2013).   I have tried using ToUniversalTime method, but it does nothing... the dates look the same: dd/mm/yyyy.

How can I compare the dates in the different locales?
0
Comment
Question by:rmmarsh
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 8

Expert Comment

by:virtuadept
ID: 38760493
Do you have some kind of string or control text that you have to convert to date and in the US the string is coming MM/DD/YYYY and in the UK it is DD/MM/YYYY and your converter is getting the UK one backwards?

Can you check the system's culture date pattern?

DateTime dtTemp = DateTime(dateString, CurrentCulture.DateTimeFormat.ShortDatePattern, CurrentCulture.DateTimeFormat);

Note that only works if the UK machines are set up to use a UK date format and its not just that the machine is US format but the data being converted isn't.
0
 

Author Comment

by:rmmarsh
ID: 38760709
I don't have any converter... I never intended this app to run anywhere outside the US, so I'm getting bitten now with localization...

DateTime(dateString... is a type but used as a variable...
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 38760808
If possible can you post some example code where you are comparing the dates? And how those are being populated?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:rmmarsh
ID: 38760819
It's very simple:  
If (DateTime.Compare(decryptedDate, DateTime.Today) > 0)

Open in new window


Both dates have the same format: dd/mm/yyyy
0
 
LVL 84

Expert Comment

by:David Johnson, CD, MVP
ID: 38761213
It greatly depends upon how you STORE the object, if you store it as a datetime object then you're free and clear, if you store it as a string then your in trouble. You can always configure HOW TO DISPLAY the object
0
 

Author Comment

by:rmmarsh
ID: 38761231
Object was stored as an encrypted string in a Firebird d/b.  I bring it out, decrypt it into a DateTime object and compare it to DateTime.Now.   It won't compare correctly...
0
 
LVL 84

Expert Comment

by:David Johnson, CD, MVP
ID: 38761422
Firebird does not have any encryption integrated http://bit.ly/WBh3my 
so you encrypt and decrypt it using your own procedure it should be stored as a BLOB SUB_TYPE 0 (which is binary)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38761591
What format are dates stored in the DB?  DD/MM/YYYY? Or does it depend on where the application was running from (e.g. if a UK user runs the app, then a UK-formatted date gets pushed to the DB).
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 38762645
If your date comparison works fine in the US where the date order is MM/DD/YYYY, but not in the UK, where the date order is DD/MM/YYYY,
then this implies your default locale is the US.
If the dates you retrieve are in string format, then why not use the Parse method of the DateTime structure and use the local variable, eg
string strDate = "";
DateTime convertedDate = DateTime.MinValue;
System.IFormatProvider prov = new System.Globalization.CultureInfo( "en-UK", true );

System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.AdjustToUniversal | System.Globalization.DateTimeStyles.AssumeLocal;

ConvertedDate = DateTime.Parse( strDate, prov, style );

Open in new window

0
 

Author Comment

by:rmmarsh
ID: 38763178
Hi Kenneth:  I sent a UK formatted date to the user to see if that fixes his problem... will post results...
Orcbighter: dates are not in string format, unless you consider an encrypted date a string.  I'm testing Kaufmed's suggestion; will post results.
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 38763214
Object was stored as an encrypted string in a Firebird d/b.  I bring it out, decrypt it into a DateTime object and compare it to DateTime.Now.   It won't compare correctly...

Ahh. Ok so you have no way of knowing whether or not the record was stored in the database as a US style MM/DD/YYYY or a UK style DD/MM/YYYY and so on the UK ones it is giving the wrong date back, is that the issue?

I don't know how you can work around this. If there was nothing else stored on the record that indicates what system or locale that the record was stored on which would allow you to know which format it was in, you just can't tell what it is. In some cases you could, obviously, if you have a 13/1/2012 then you know that's a UK one, but 1/2/2012 could either be Jan 2 2012 or Feb 1 2012 depending on who entered it on what system.
0
 

Author Comment

by:rmmarsh
ID: 38763246
virtuadept:  I could look at the current locale, right?  And then do the compare based on the locale.  A lot depends on if the encrypted license key (nothing more than the date) using the UK locale works...
0
 
LVL 8

Assisted Solution

by:virtuadept
virtuadept earned 1000 total points
ID: 38775263
Sorry for the delay. Yes, you can look at current locale if this is a database that is on their specific machines. I thought maybe you had different client applications in different locales entering data into a remote database, but they weren't all entering the date data the same way due to locale. That's still solvable, but you'd have to solve it when doing the inserts, not on the reading end, and cleaning up the wrong data in there now would be tricky.
0
 
LVL 84

Accepted Solution

by:
David Johnson, CD, MVP earned 1000 total points
ID: 38776487
a date is stored as a date value it is not a string.. i.e. so many days since Jan 01, 1980.. I think your problem is in the globalization of your application ergoYou need to internationalize your application.
0
 

Author Closing Comment

by:rmmarsh
ID: 38788752
Thank you...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question