?
Solved

How to compare 2 DateTime objects in different locale?

Posted on 2013-01-09
15
Medium Priority
?
271 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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 82

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 82

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 82

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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

800 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