2

I have a program that has synchronization. That means I need to save the last synchronization date and check if it needs to be synchronized.

So, I have this:

IS.SaveContactsRetrieveDate(DateTime.Now.ToString("dd.MM.yyyy")); 

Saving a date to Isolated Storage.

Then, when I call IF:

DateTime toDate = DateTime.Now; string contactsRetriveDate = IS.ReadContactsRetriveDate(); if (contactsRetriveDate == "" || DateTime.Compare(toDate, DateTime.Parse(contactsRetriveDate)) == 1) { MessageBox.SHow(""); } 

The problem is that when user changes the region code fails here:

DateTime.Compare(toDate, DateTime.Parse(contactsRetriveDate)) 

With incorrect input error.

I understand that Latvian format is dd.MM.yyyy and USA MM/dd/yyyy - but I can't find a solution...

I need all datetime parsed in one format, so I could add days, weeks and compare date.

1
  • Well you can use DateTime.ParseExact with the format Commented Jul 15, 2013 at 9:57

3 Answers 3

2

You should serialize and deserialize your date in a culture-independent manner (where "d" is the "Short date pattern" of the Standard Date and Time Format Strings):

var s = DateTime.Now.ToString("d", CultureInfo.InvariantCulture); var d = DateTime.Parse(s, CultureInfo.InvariantCulture); 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use ParseExact

DateTime.ParseExact(datestring, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture); 

you already know format so you can go for this, but make sure the string is in same format and never changes.

Comments

1

u can try this one:

 DateTime toDate = DateTime.Now; string contactsRetriveDate = IS.ReadContactsRetriveDate(); DateTime contactsRetriveDat = Convert.ToDateTime(contactsRetriveDate); if (contactsRetriveDate == "" || toDate.CompareTo(contactsRetriveDat)==0) { MessageBox.SHow(""); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.