I have json where date is defined like this;
{\"$date\":\"1947-11-13T00:00:00.000Z\"}
and my deserilising code look like this:
return JsonConvert.DeserializeObject<U>(szResp,
new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd"
});
The object I'm creating is just
[JsonProperty(PropertyName = "$date")]
public string date { get; set; }
but instead of 13/11/1947 00:00:00
I'm receiving : 11/13/1947 00:00:00
I have no clues why this deserilizer behaves like this and changes data format.
More over if I try to see data in Json visualizer in visual studio my date is shown properly "13/11/1947 12:00:00 πμ"
Related
I'm working with a DateOnly variable and I'm trying to get the DateTime.Now time in a dd/mm/yyyy format, however it's only returning the date on mm/dd/yyyy format.
I strictly need the current date in a dd/mm/yyyy format, and I haven't been able to figure it out how to.
This is an example how I'm working to convert the DateTime.Now to DateOnly type
public class Example
{
public DateOnly? Date{get; set;}
}
public class Process1
{
Example example = new Example();
{
example.Date= DateOnly.FromDateTime(DateTime.Now);
//this is returning the current date in a mm/dd/yyyy format
}
}
Formatting can only be done by string not by date only.
save date in dateonly datatype
example.Date= DateOnly.FromDateTime(DateTime.Now);
but when you need specify format then use string like below
string s = example.Date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
or
s = example.Date.ToString("dd/MM/yyyy");
For More detail refer this Link
I set the Date variable as a DateTime property in the Example Class :
public DateTime Date { get; set; } = DateTime.Now;
In the main code, i converted the Date property into a string and assigned it to dateOnly Variable:
string dateOnly = Convert.ToString(example.Date.ToString("dd-mm-yyyy"));
I would like serialize object to XML. This part is easy.
But i would serialize properties (like DateTime) with a specific format.
My current code is this one :
[XmlIgnore]
private DateTime _paymentDate { get; set; }
public string PaymentDate
{
get
{
return this._paymentDate.ToString("yyyy-MM-dd HH:mm:ss");
}
set
{
this._paymentDate = DateTime.ParseExact(value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
}
But it's very bad. If i have 100 DateTime properties, i should duplicate this code 100 times.
So, i would like to use a formatter (like with Newtonsoft JSON).
How can i do it ?
Thanks a lot :)
I have a service which returns a json like this :
[
{
"title":"First event",
"startDate":"\/Date(1495512000000-0400)\/",
"endDate":"\/Date(1495857540000-0400)\/",
}
]
I deserialize the json output to the list with respect to the model I have defined :
Model
public class EventModel
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("startDate")]
public DateTime StartDate { get; set; }
[JsonProperty("endDate")]
public DateTime EndDate { get; set; }
}
C# Code of deserialization
I am using Newtonsoft.Json package for deserialization.
List< EventModel > lstEvent = new List< EventModel >();
lstEvent = JsonConvert.DeserializeObject<List< EventModel >>(await objMyServices.GetData());
So in this case when I debug I get the following output in two different timezone:
For GMT+5:30 (Kolkata - India):
Start Time : 23-05-2017 09:30:00
End Time : 27-05-2017 09:29:00
For GMT-4:00 (New York):
Start Time : 23-05-2017 00:00:00
End Time : 26-05-2017 23:59:00
Therefore for every record, start time is not parsed to the respective system time zone when I switch to GMT-4:00 (New York).
What is the thing I am missing here ?
Json.NET allows you to specify what kind of DateTime to return (UTC, Local) through the DateTimeZoneHandling serializer setting. The default is Local.
To return a UTC DateTime, use DateTimeZoneHandling.Utc :
var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
var content=await objMyServices.GetData();
var lstEvent = JsonConvert.DeserializeObject<List<EventModel>>(content, settings);
Perhaps a better option is to change the property types to DateTimeOffset. This type preserves the original offset.
You can use Ticks in Web API for this. This is much convenient when you are working with multiple time zones. Refer below example .
DateTime.UtcNow.Ticks
I am working with the you-tube API, which returns JSON data. The video published date is in this format: "publishedAt": "2017-04-30T18:18:41.000Z".
After deserializing the JSON object, I want to get the date from the published DateTime in C#.
How can I do it, and what is this format of DateTime?
There's absolutely no need to manually parse a well-formatted ISO 8601 date.
Simply change the property on your model from string to DateTime:
public class VideoData
{
[JsonProperty("publishedAt")]
public DateTime PublishedAt { get; set; }
}
And then deserialize into that:
var model = JsonConvert.DeserializeObject<VideoData>(jsonString);
And Json.NET will handle the rest.
class className
{
Datetime dateTime {get; set;}
}
className object = new className();
When I use Json(object) this returns the dateTime field in this format :
{"dateTime":"/Date(1486516302715)/"}
But what I want is to return the same value using an attribute to the dateTime parameter instead.
Something like :
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
Datetime dateTime {get; set;}
Only the JavaScriptDateTimeConverter is not the one which gives me the date in the right format.
In javascript you could use the new Date method.
new Date(parseInt(date.substr(6))); // date= "/Date(1486516302715)/"