DateTime.ToString() format that can be used in a filename or extension?

前端 未结 8 2104
北恋
北恋 2020-12-12 16:53

I want to add a timestamp to filenames as files are created but most of the DateTime methods I\'ve tried output something with spaces and slashes. For instance:



        
8条回答
  •  悲哀的现实
    2020-12-12 17:26

    The below list of time format specifiers most commonly used.,

    dd -- day of the month, from 01 through 31.

    MM -- month, from 01 through 12.

    yyyy -- year as a four-digit number.

    hh -- hour, using a 12-hour clock from 01 to 12.

    mm -- minute, from 00 through 59.

    ss -- second, from 00 through 59.

    HH -- hour, using a 24-hour clock from 00 to 23.

    tt -- AM/PM designator.

    Using the above you will be able to form a unique name to your file name.

    Here i have provided example

    string fileName = "fileName_" + DateTime.Now.ToString("MM-dd-yyyy_hh-mm-ss-tt") + ".pdf";
    

    OR

    If you don't prefer to use symbols you can try this also.,

    string fileName = "fileName_" + DateTime.Now.ToString("MMddyyyyhhmmsstt") + ".pdf";
    

    Hope this helps to someone now or in future. :)

提交回复
热议问题