Date & Time to C#

Hi,

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

@balaraj.pubalan

/ is not supported in filepath names

so please use MMddyyyy hhmm tt

cheers

1 Like

@balaraj.pubalan
: 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

Hi,

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

"C:\\Users\Documents\\DL_"

OR

@"C:\Users\Documents\DL_" 

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")

Regards,

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

Thanks!

1 Like

Its works :slight_smile:

2 Likes

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