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. .NET (Core and Framework)
  4. Date formatting issue

Date formatting issue

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestion
4 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.
  • J Offline
    J Offline
    Jack Vanderhorst
    wrote on last edited by
    #1

    I'm using

    DateTime.Now.ToString("M/d/yyyy")

    to try and format today's date. I've noticed that when I change my regional settings through control panel, it changes the result I get from this method. When I set my default date format to dd-MMM-yy in Control Panel, it changes the output of my ToString method from containing slashes (as I've specified in my format string) to dashes, which I can only assume are coming from my regional settings. I thought the point of specifying my own format string was so that I could, oh I dunno, specify my own format?! Are my control panel settings really affecting this method call? Why in the heck? I realize I can just code my own cheesey routine to format dates and avoid the issue, but I'd like to understand what's going on here.

    L 1 Reply Last reply
    0
    • J Jack Vanderhorst

      I'm using

      DateTime.Now.ToString("M/d/yyyy")

      to try and format today's date. I've noticed that when I change my regional settings through control panel, it changes the result I get from this method. When I set my default date format to dd-MMM-yy in Control Panel, it changes the output of my ToString method from containing slashes (as I've specified in my format string) to dashes, which I can only assume are coming from my regional settings. I thought the point of specifying my own format string was so that I could, oh I dunno, specify my own format?! Are my control panel settings really affecting this method call? Why in the heck? I realize I can just code my own cheesey routine to format dates and avoid the issue, but I'd like to understand what's going on here.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, a typical mistake. This page of MSDN documentation[^] tells you what the parts of the format string represent; M is an indicator for the month number, and / for the date separator. As a result regional settings are still infiltrating your app! You could create your own DateTimeFormatInfo object and set all of its details any way you like, then pass that to DateTime.ToString() instead of a string. [ADDED] Or you could single quotes to escape the special characters you want to be taken literally, so use '/' to get a real slash, no matter what. [/ADDED] :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      modified on Friday, December 25, 2009 3:46 PM

      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, a typical mistake. This page of MSDN documentation[^] tells you what the parts of the format string represent; M is an indicator for the month number, and / for the date separator. As a result regional settings are still infiltrating your app! You could create your own DateTimeFormatInfo object and set all of its details any way you like, then pass that to DateTime.ToString() instead of a string. [ADDED] Or you could single quotes to escape the special characters you want to be taken literally, so use '/' to get a real slash, no matter what. [/ADDED] :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        modified on Friday, December 25, 2009 3:46 PM

        J Offline
        J Offline
        Jack Vanderhorst
        wrote on last edited by
        #3

        Well I'll be, thanks.

        L 1 Reply Last reply
        0
        • J Jack Vanderhorst

          Well I'll be, thanks.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You're welcome. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          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