How to efficiently print only date and time (not seconds) in C #?

I am using a property LastWriteTime

on the file I am using to print the last modified time. However, here when I use I get a value like

"2/12/2011 11:58:45 AM".

(i) Can you help ignore the seconds here, meaning the output should look like this:    2/12/2011 11:58 AM

(ii) Also, how can I ensure that I am dealing with timezones as the LastWriteTime file can be like other GEO timings. Can you help take care of this?

(iii) Also how to handle culture issues here as CurrentCulture will be different for machines?


source to share

2 answers

Templates for DateTime.ToString ('g'):

0    MM/dd/yyyy HH:mm    08/22/2006 06:30

1    MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM

2    MM/dd/yyyy H:mm     08/22/2006 6:30

3    MM/dd/yyyy h:mm tt  08/22/2006 6:30 AM


Source: DateTime.ToString Templates



You can use the format g

to represent the date as you wish:

 string date = File.GetLastWriteTime("file").ToString("g");


As for TimeZone

, DateTime

does not contain relevant information TimeZone

. You have to deal with this yourself.

Maybe this will help you:



All Articles