Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Date in c# in a specified format

Tags:

c#

.net

c#-4.0

I am using the .NET DateTime type and would like to print date in the following format

"2017-08-23T11:33:14.653191-05:00"

I am currently able to get the date with year month and day by using this.

DateTime.UtcNow.ToString("yyyy-MM-dd")

The above code results in "2017-08-23", but I want the date in above format.

like image 678
Adithya Sai Avatar asked Aug 23 '17 05:08

Adithya Sai


People also ask

Is there print in C?

In C programming language, printf() function is used to print the (“character, string, float, integer, octal and hexadecimal values”) onto the output screen. We use printf() function with %d format specifier to display the value of an integer variable.

What is %d in printf in C?

%d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int .


1 Answers

You can use a custom format string to achieve this by using DateTime Now with a format

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.ffffffK");

Please see here. Parts of the format string are:

  • yyyy is the four digit year
  • MM the month (numerical)
  • dd the days of the month
  • HH the hours (24 hours format)
  • ss the seconds
  • ffffff the millionths of a second
  • K the time zone
like image 151
Paul Kertscher Avatar answered Oct 19 '22 22:10

Paul Kertscher