CultureInfo and DateTime settings
-
Gents, I have the following problem that drives me insane. I've written a service that runs in the background. However, I can't get the
DateTime
format right. I must have tried everything to get it right, but to no avail. When the service starts or stops, it will tell me by writing to a log file. The output of the log file looks like this :1/19/2009 1:48:40 PM Manager: Service Stopped 1/19/2009 1:48:43 PM Manager: Service Started 1/19/2009 2:17:05 PM Manager: Service Stopped 1/19/2009 2:17:29 PM Manager: Service Started 1/19/2009 2:18:02 PM Manager: Service Stopped 1/19/2009 2:20:27 PM Manager: Service Started
Now .. When it writes to the logfile, it uses the above format forDateTime.Now
, while I want to use "dd-mm-yyyy HH:mm:ss" I set the regional setting in Control Panel as well as the Date and Time format. It shows up perfect in my system clock. It still spits out the above to the logfile. Then I tried the following in my code, so set it programmatically :// The main entry point for the process static void Main() { //Set the culture CultureInfo newCulture = new CultureInfo("en-US", false); newCulture.DateTimeFormat.FullDateTimePattern = "dd-mm-yyyy HH:mm:ss"; System.Threading.Thread.CurrentThread.CurrentCulture = newCulture; System.ServiceProcess.ServiceBase\[\] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = New System.ServiceProcess.ServiceBase\[\] {new WinService1(), new ySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase\[\] { new RealTime() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); }
That didn't fix it .. still the same output. When I put a
MessageBox
in between somewhere to let me showDateTime.Now.ToString()
, it also comes back with 1/19/2009 2:20:27 PM. Some of the methods in the service report an invalid <code>DateTime</code> conversion when I try the following code :DateTime time = Convert.ToDateTime("19-01-2009 13:12:56");
It drives me insane. Can please someone point me in the right direction ? Kind regards,
-
Gents, I have the following problem that drives me insane. I've written a service that runs in the background. However, I can't get the
DateTime
format right. I must have tried everything to get it right, but to no avail. When the service starts or stops, it will tell me by writing to a log file. The output of the log file looks like this :1/19/2009 1:48:40 PM Manager: Service Stopped 1/19/2009 1:48:43 PM Manager: Service Started 1/19/2009 2:17:05 PM Manager: Service Stopped 1/19/2009 2:17:29 PM Manager: Service Started 1/19/2009 2:18:02 PM Manager: Service Stopped 1/19/2009 2:20:27 PM Manager: Service Started
Now .. When it writes to the logfile, it uses the above format forDateTime.Now
, while I want to use "dd-mm-yyyy HH:mm:ss" I set the regional setting in Control Panel as well as the Date and Time format. It shows up perfect in my system clock. It still spits out the above to the logfile. Then I tried the following in my code, so set it programmatically :// The main entry point for the process static void Main() { //Set the culture CultureInfo newCulture = new CultureInfo("en-US", false); newCulture.DateTimeFormat.FullDateTimePattern = "dd-mm-yyyy HH:mm:ss"; System.Threading.Thread.CurrentThread.CurrentCulture = newCulture; System.ServiceProcess.ServiceBase\[\] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = New System.ServiceProcess.ServiceBase\[\] {new WinService1(), new ySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase\[\] { new RealTime() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); }
That didn't fix it .. still the same output. When I put a
MessageBox
in between somewhere to let me showDateTime.Now.ToString()
, it also comes back with 1/19/2009 2:20:27 PM. Some of the methods in the service report an invalid <code>DateTime</code> conversion when I try the following code :DateTime time = Convert.ToDateTime("19-01-2009 13:12:56");
It drives me insane. Can please someone point me in the right direction ? Kind regards,
You can try this line: DateTime.Now.ToString("MMM dd,yy HH:mm:ss", CultureInfo.InvariantCulture) I think it will solve your problem:cool:
-
You can try this line: DateTime.Now.ToString("MMM dd,yy HH:mm:ss", CultureInfo.InvariantCulture) I think it will solve your problem:cool:
Thank you for your feedback, it does indeed fix half of my problem. I can get the date right in the logfile, however .. it will not fix the below calculation :
DateTime lastTime = Convert.ToDateTime(dt.Rows[lastRow]["date"].ToString());
Where the value in the DataTable is "19-01-2009 13:45:59", the error is :
1/19/2009 4:34:24 PM System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at Loudnixx_RealTime.RealTimeRadar.ExceedTrackTime()
at Loudnixx_RealTime.RealTimeRadar.ProcessString(String dat)
at Loudnixx_RealTime.RealTimeRadar.Listen()Somehow there has to be a way to set it globally throughout the program. Can you point me in the right direction ? kind regards,
-
Thank you for your feedback, it does indeed fix half of my problem. I can get the date right in the logfile, however .. it will not fix the below calculation :
DateTime lastTime = Convert.ToDateTime(dt.Rows[lastRow]["date"].ToString());
Where the value in the DataTable is "19-01-2009 13:45:59", the error is :
1/19/2009 4:34:24 PM System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at Loudnixx_RealTime.RealTimeRadar.ExceedTrackTime()
at Loudnixx_RealTime.RealTimeRadar.ProcessString(String dat)
at Loudnixx_RealTime.RealTimeRadar.Listen()Somehow there has to be a way to set it globally throughout the program. Can you point me in the right direction ? kind regards,
It looks like a different date format: 1/1/2008 as pose to 1-1-2008 and in any case you can manipulate only on the strings.....
-
Thank you for your feedback, it does indeed fix half of my problem. I can get the date right in the logfile, however .. it will not fix the below calculation :
DateTime lastTime = Convert.ToDateTime(dt.Rows[lastRow]["date"].ToString());
Where the value in the DataTable is "19-01-2009 13:45:59", the error is :
1/19/2009 4:34:24 PM System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at Loudnixx_RealTime.RealTimeRadar.ExceedTrackTime()
at Loudnixx_RealTime.RealTimeRadar.ProcessString(String dat)
at Loudnixx_RealTime.RealTimeRadar.Listen()Somehow there has to be a way to set it globally throughout the program. Can you point me in the right direction ? kind regards,
Is dt.Rows[lastRow]["date"] already a DateTime? If yes, don't convert it into a string again. For converting DateTimes to strings, use DateTime.ToString(formatString). For parsing strings into DateTimes, use DateTime.ParseExact[^]
Cheers, Vıkram.
I don't suffer from insanity, I enjoy every moment of it.
-
Is dt.Rows[lastRow]["date"] already a DateTime? If yes, don't convert it into a string again. For converting DateTimes to strings, use DateTime.ToString(formatString). For parsing strings into DateTimes, use DateTime.ParseExact[^]
Cheers, Vıkram.
I don't suffer from insanity, I enjoy every moment of it.
Thank you all for your reply :)
DateTime lastTime = DateTime.ParseExact(dt.Rows[lastRow]["date"].ToString(),"dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
did the trick :) Once again, thank you kind regards,
-
Thank you all for your reply :)
DateTime lastTime = DateTime.ParseExact(dt.Rows[lastRow]["date"].ToString(),"dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
did the trick :) Once again, thank you kind regards,
Like I said, if dt.Rows[lastRow]["date"] is already a DateTime, you just have to assign it to lastTime. If it's not, remove the extra ToString(). You are welcome :) Also, if you think somebody helped you, please vote them a 5. It's a small pleasure ;)
Cheers, Vıkram.
I don't suffer from insanity, I enjoy every moment of it.