Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. CultureInfo and DateTime settings

CultureInfo and DateTime settings

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    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 for DateTime.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 show DateTime.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,

    A 1 Reply Last reply
    0
    • R Rick van Woudenberg

      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 for DateTime.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 show DateTime.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,

      A Offline
      A Offline
      Arish rivlin
      wrote on last edited by
      #2

      You can try this line: DateTime.Now.ToString("MMM dd,yy HH:mm:ss", CultureInfo.InvariantCulture) I think it will solve your problem:cool:

      R 1 Reply Last reply
      0
      • A Arish rivlin

        You can try this line: DateTime.Now.ToString("MMM dd,yy HH:mm:ss", CultureInfo.InvariantCulture) I think it will solve your problem:cool:

        R Offline
        R Offline
        Rick van Woudenberg
        wrote on last edited by
        #3

        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,

        A V 2 Replies Last reply
        0
        • R Rick van Woudenberg

          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,

          A Offline
          A Offline
          Arish rivlin
          wrote on last edited by
          #4

          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.....

          1 Reply Last reply
          0
          • R Rick van Woudenberg

            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,

            V Offline
            V Offline
            Vikram A Punathambekar
            wrote on last edited by
            #5

            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.

            R 1 Reply Last reply
            0
            • V Vikram A Punathambekar

              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.

              R Offline
              R Offline
              Rick van Woudenberg
              wrote on last edited by
              #6

              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,

              V 1 Reply Last reply
              0
              • R Rick van Woudenberg

                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,

                V Offline
                V Offline
                Vikram A Punathambekar
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups