Date & Time to C#


How to make the excel file generate with date & Time?i have error as below. Thanks.


/ is not supported in filepath names

so please use MMddyyyy hhmm tt


1 Like

: and / are not supported in file name.

DateTime.Now.ToString("MMddyyyy hhmmtt)

1 Like

Hi @balaraj.pubalan

“C:\Users\User\Documents\DL_”+ DateTime.Now.ToString + “.xlsx”

Hope it helps!!

1 Like


In C#, as backslash is special character, it’s necessary to escape backslash or use @ as the following




In addition, / and : cannot be used as file name (or file path if you want to use it as part of directory name ). So as a result,the following will work, for example.

@"C:\Users\Documents\DL_" +DateTime.Now.ToString("MMddyyyy-hhmm tt")


1 Like

@balaraj.pubalan As mentioned by above responses, backslash \ and slash / are illegal characters as part of filenames. I would just add that when working with dates in filenames the ISO 8601 format works best yyyy-MM-dd

It doesn’t use slashes, unambiguous and is an actual standard. This also has a positive effect of sorting your files chronologically (regardless of creation/access time properties) when sorted alphabetically.

Know more about why people should be using this format more: ISO - ISO 8601 — Date and time format


1 Like

Its works :slight_smile:


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.