[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to compare 2 DateTime objects in different locale?

Posted on 2013-01-09
15
Medium Priority
?
272 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 83

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 83

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 83

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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