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. Datetime regional format

Datetime regional format

Scheduled Pinned Locked Moved C#
help
2 Posts 2 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.
  • S Offline
    S Offline
    sevan
    wrote on last edited by
    #1

    when i write the date in win.txt it was like this Lsbox_Date.Items.Add(DTP_Today.Value.ToShortDateString()); so if the regional setting was set to MM/dd/yyyy it will write the date the same as regional setting so i change it to this which always gonna be dd/mm/yyyy Lsbox_Date.Items.Add(DTP_Today.Value.Day.ToString()+ "/"+DTP_Today.Value.Month.ToString()+"/"+DTP_Today.Value.Year.ToString()); when i read the win.txt this code is reading the date from win.txt StreamReader Read_key = File.OpenText(@windir+"\\Win.txt"); string key_Trim; string key; key_Trim = Read_key.ReadLine(); key = key_Trim.TrimEnd(); Read_key.Close(); Convert the key to datetime DTP_Key.Value = Convert.ToDateTime(key.ToString()); so if the regional setting is set to dd/mm/yyyy i have no Problem coz the format is the same but if the regional setting is set to mm/dd/yyyy then i get an error converting the key string. i need help pls ASAP

    S 1 Reply Last reply
    0
    • S sevan

      when i write the date in win.txt it was like this Lsbox_Date.Items.Add(DTP_Today.Value.ToShortDateString()); so if the regional setting was set to MM/dd/yyyy it will write the date the same as regional setting so i change it to this which always gonna be dd/mm/yyyy Lsbox_Date.Items.Add(DTP_Today.Value.Day.ToString()+ "/"+DTP_Today.Value.Month.ToString()+"/"+DTP_Today.Value.Year.ToString()); when i read the win.txt this code is reading the date from win.txt StreamReader Read_key = File.OpenText(@windir+"\\Win.txt"); string key_Trim; string key; key_Trim = Read_key.ReadLine(); key = key_Trim.TrimEnd(); Read_key.Close(); Convert the key to datetime DTP_Key.Value = Convert.ToDateTime(key.ToString()); so if the regional setting is set to dd/mm/yyyy i have no Problem coz the format is the same but if the regional setting is set to mm/dd/yyyy then i get an error converting the key string. i need help pls ASAP

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Use a explicit CultureInfo for writing and reading the date to be independent from setting of the PC:

      System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US"); // use culture of your choice here

      DTP_Today.Value.ToString("d", culture); // get short date string in culture-specific formatting

      Convert.ToDateTime(key.ToString(), culture); // create from short date string in culture-specific formatting


      www.troschuetz.de

      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