Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Could you please help me that how to convert datetime format like below

2017-04-26T12:30:16+08:00


I want to pass the above datetime format exactly

What I have tried:

I have tried with the below code but not getting with +
2017-04-26T12:30:16+08:00


DateTime dt = new DateTime();
            dt = DateTime.Now;
 Label13.Text = String.Format("{0:s}", dt);


2017-04-26T12:30:16
Posted
Updated 7-Jun-17 5:36am
v2
Comments
ZurdoDev 7-Jun-17 10:50am    
If you have a DateTime object you can call .ToString() and give a format inside ToString()
DGKumar 7-Jun-17 11:20am    
DateTime dt = new DateTime();
dt = DateTime.Now;
Label13.Text = String.Format("{0:s}", dt);
2017-04-26T12:30:16 but i need like below
2017-04-26T12:30:16+08:00 with +08:00
Richard MacCutchan 7-Jun-17 12:12pm    
You could easily find the answer to this by just reading the DateTime class documentation.

1 solution

Try this:
C#
DateTime d = new DateTime(2017, 6, 7, 13, 14, 15, DateTimeKind.Utc);
string s = d.ToLocalTime().ToString("yyyy-MM-dd HH:mm:sszzz");
Console.WriteLine(s);

It produces this:
2017-06-07 14:14:15+01:00
because I'm on UK time - currently BST, one hour ahead of UTC.
 
Share this answer
 
Comments
DGKumar 7-Jun-17 14:22pm    
thank you now i am able get perfect format
OriginalGriff 7-Jun-17 14:27pm    
You're welcome!
Adityakumar2318 8-Jun-17 1:56am    
Great answer. 5 out of 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900