Solved

How to compare 2 DateTime objects in different locale?

Posted on 2013-01-09
15
260 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
Comment Utility
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
Comment Utility
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
Comment Utility
If possible can you post some example code where you are comparing the dates? And how those are being populated?
0
 

Author Comment

by:rmmarsh
Comment Utility
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 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
Comment Utility
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 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 78

Accepted Solution

by:
David Johnson, CD, MVP earned 250 total points
Comment Utility
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
Comment Utility
Thank you...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now