Solved

C# DateTime format

Posted on 2016-08-25
5
52 Views
Last Modified: 2016-09-12
Hi all,

Can we represent DateTime in C# in below format. I haven't found this format in .Net

2016-08-31T15:00:00-04:00

Here is -4 is UTC offset.  The time is local EST time

 I want to serialize the DateTime to Json in the above format. How do we convert to this format.

Thanks in advance
0
Comment
Question by:Member_2_7967608
  • 3
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41771086
Assuming that the DateTime is from a DateTime object you can use the ToString to get it in that format like this
var dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm-ss:ffff"));

Open in new window

0
 
LVL 2

Assisted Solution

by:Kyaw Wanna
Kyaw Wanna earned 500 total points
ID: 41771226
Please do the following steps to convert into json serialization.

 - Please use Newtonsonft.Json and Newtonsoft.Json.Converters to serialize JSON format.

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Open in new window


- Create Model Class for converting to Json Serialization

 
public class clsDateTime
{
      public DateTime attrDateTime { get; set; }
}

Open in new window


 - Convert UTC time to Eastern Time Zone

 
//--- Use this code in your program for converting to EST Time ----
var utcTime = DateTime.UtcNow;
TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime estTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, estZone);

Open in new window


 - Convert to JSON format using Newtonsoft.Json
 
//--- Convert To Json Serialize Format
clsDateTime objDatetime = new clsDateTime { attrDateTime = estTime };
string isoJson = JsonConvert.SerializeObject(objDatetime, new IsoDateTimeConverter());

Open in new window

0
 

Author Comment

by:Member_2_7967608
ID: 41771759
This is my code. How can I set the formatter here.  The datetime is coming from UI as "req2".


 var request = new RestRequest("api/ACSSchedule/", Method.POST) { RequestFormat = DataFormat.Json };
            request.JsonSerializer = new RestSharp.Newtonsoft.Json.NewtonsoftJsonSerializer();
            request.AddBody(req2);
            var response = Execute(request, _client);
0
 

Accepted Solution

by:
Member_2_7967608 earned 0 total points
ID: 41771852
I think NewtonJson serializer is able to format in yyyy-MM-ddTHH:mm-ss:zzz

i.e. represnted as

2016-08-31T15:00:00-04:00
0
 

Author Closing Comment

by:Member_2_7967608
ID: 41793918
Formatting works with Newtonsoft Json serailizer
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question