Solved

How to compare 2 DateTime objects in different locale?

Posted on 2013-01-09
15
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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 81

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 81

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 250 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 81

Accepted Solution

by:
David Johnson, CD, MVP earned 250 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

Independent Software Vendors: 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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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