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. C# format date

C# format date

Scheduled Pinned Locked Moved C#
csharptutorialquestion
7 Posts 6 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.
  • C Offline
    C Offline
    classy_dog
    wrote on last edited by
    #1

    In a C# 2008 application, I will obtain the current date by using the statement:

    DateTime.Now;

    I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?

    R L K J K 5 Replies Last reply
    0
    • C classy_dog

      In a C# 2008 application, I will obtain the current date by using the statement:

      DateTime.Now;

      I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      See this[^] link. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • C classy_dog

        In a C# 2008 application, I will obtain the current date by using the statement:

        DateTime.Now;

        I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        string date = DateTime.Now.ToString("dd/MM/yyyy");
        string time = DateTime.Now.ToString("hh:mm:ss");

        J 1 Reply Last reply
        0
        • C classy_dog

          In a C# 2008 application, I will obtain the current date by using the statement:

          DateTime.Now;

          I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          Ravi has provided you with a link that should answer your question, so I'm not going to add to it. I'd like to make a suggestion as well. If you can, change the format of the date: yyyy-MM-dd yyyy_MM_dd or yyyymmdd or somesuch The format you have can be ambiguous between US and European date formats. This might not be a problem in itself (you may only ever deal in US dates). The other thing changing the date will do is allow you to sort the files by date via their filenames.

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
          -Or-
          A Dead ringer for Kate Winslett[^]

          1 Reply Last reply
          0
          • C classy_dog

            In a C# 2008 application, I will obtain the current date by using the statement:

            DateTime.Now;

            I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            You would use a DateFormat string of MM_dd_yy, which can either be passed to the ToString method on a DateTime:

            var formatted = DateTime.Now.ToString("MM_dd_yy")

            or used in a larger format string, say for example if you're building up a path:

            var path = String.Format("Some/Directory_{0:MM_dd_yy}/somefile.txt",DateTime.Now);

            Here's a live example of the latter: http://rextester.com/UTHQ72076[^]

            1 Reply Last reply
            0
            • L Lost User

              string date = DateTime.Now.ToString("dd/MM/yyyy");
              string time = DateTime.Now.ToString("hh:mm:ss");

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              This only slightly answers the OP question, and the bit it does get right, uses the wrong format for what the OP asked.

              1 Reply Last reply
              0
              • C classy_dog

                In a C# 2008 application, I will obtain the current date by using the statement:

                DateTime.Now;

                I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?

                K Offline
                K Offline
                KiranKumar Roy
                wrote on last edited by
                #7

                Try this.

                protected string FormatDate()
                {
                string strTemp;
                strTemp = DateTime.Now.ToString("MM/dd/yy").Replace("/", "_");
                return strTemp;
                }

                KiranKumar Roy

                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