1

I have written a windows service which will use current date from system. I am getting error, only after installation of the service. but when i run the service in debug mode, its not giving any error and its working good. my current system date is displaying as 20 April 2016. I have tried using CultureInfo.InvariantCulture still I have the same Problem.

"String was not recognized as a valid DateTime."

string startDate = DateTime.ParseExact(TodayDate, "dd/MM/yyyy", CultureInfo.CurrentCulture)
                                      .ToString("yyyy-MM-dd"); 

only this line giving error. i checked TodayDate value is in dd/MM/yyyy 20/04/2016 which is perfect.

string TodayDate = System.DateTime.Now.ToString("dd/MM/yyyy");
string startDate = DateTime.ParseExact(TodayDate, "dd/MM/yyyy", CultureInfo.CurrentCulture).ToString("yyyy-MM-dd");

enter image description here

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tan
  • 778
  • 3
  • 18
  • 36
  • This code seems to work https://dotnetfiddle.net/HdDJkw Can you show the culture date time setting of your installation machine? – Pavel Kharibin Apr 20 '16 at 05:20
  • And in your code you use CultureInfo.CurrentCulture – Pavel Kharibin Apr 20 '16 at 05:29
  • i have added picture, but my windows service is aimed to redistribute to different timezones with different settings how i should handle this. – Tan Apr 20 '16 at 05:56
  • To handle different time zones use utc date. – Pavel Kharibin Apr 20 '16 at 06:20
  • And one more. What is the reason of getting first date string in one format and then getting second date string by parsing first to datetime and then to stringing it in another format? – Pavel Kharibin Apr 20 '16 at 06:23
  • The United States? - perhaps `MM/dd/yyyy` would do... – Vojtěch Dohnal Apr 20 '16 at 06:29
  • Why are you fiddling about with *strings* at all? Generally, you should only be using strings when taking initial input from users or when presenting data back to the user. Everywhere else, you should just be using datetime data types, which don't *have* any format. Given that services don't have any UI, where else would you *want* to be working with a string (and *introducing* formatting issues for yourself)? – Damien_The_Unbeliever Apr 20 '16 at 06:37
  • my datetime values are stored to a xml file which is produced by a windows application, my windows service will read the xml file and then based on the values from xml file get data from ms sql database and generate a excel sheet based on every day, month, year and every day particular times set by user. that is the reason i have to handle with strings and convert to datetime and pass to sql – Tan Apr 20 '16 at 06:53
  • http://stackoverflow.com/questions/254753/what-is-the-correct-format-to-use-for-date-time-in-an-xml-file – Hans Passant Apr 20 '16 at 07:03
  • i checked the log i am using format 'dd/MM/yyyy' but the log file is having ' MM/dd/yyyy' i am using timer elapsed event can i set the cultureinfo on timer elapsed event – Tan Apr 20 '16 at 07:03
  • you are getting me wrong @Hans Passant my code is working fine i have no problem storing date and retrieving date and time values from xml, i have speciefied in my code dd/mm/yyyy (en-gb)as the format but my deployment system uses (MM/dd/yyyy) and i am doing parseexact is causing the problem, is there any way to set the culture to en-gb no matter what ever the culture the system is using i should get in en-gb format only – Tan Apr 20 '16 at 07:07
  • 3
    There is only one format that is appropriate for dates in an XML file. Doing anything else begets the usual misery. Take no prisoners, fix your code and/or reject the XML file. – Hans Passant Apr 20 '16 at 07:11

0 Answers0