Json to DateTime - change format - javascript

I send from JS (with JSON) to server (C#) a string date in format "dd/mm/yyyy" - for example "23/10/2014", but C# DateTime gets null. If I send "10/23/2014" it works.
I use MVC4.
How can I change that format that DateTime gets to I'll be able to send "23/10/2014"?

Not a direct answer to your question but I think you're safer to send the date as: yyyy/mm/dd
Then you don't need to worry about internationalisation. MVC will parse this correctly.
EDIT
Matt made a good comment below. The format of the date should be: yyyy-mm-dd which is in agreement with the ISO 8601 standard.

you can specify the date format in the serialization settings
var jsonString = #"{'ID':'1','Date':'23/10/2014'}";
JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
DateFormatString = "dd/MM/yyyy"
};
var result = JsonConvert.DeserializeObject<TheType>(jsonString, jSettings);

Related

Convert date and time format from PHP to JavaScript

I have a problem that in my MySQL, the date and time data is saved as Y-mm-dd H:m:s. How can I switch to the date and time to JavaScript?
Your date is saved as date in sql.
When you have a backend, an ajax request from browser can get a date or bigger structure as json (serialized as string). Sample:
{ date: "2020-05-23T16:29:48.973Z" }..
This date can be (should be) in an iso format, see ISO 8601 .
This date string can be parsed by javascript easily:
let dateObj = new Date("2020-05-23T16:29:48.973Z");
This object can be formatted (object -> string) with help of Intl.DateTimeFormat.
let str = new Intl.DateTimeFormat('en-US').format(dateObj);
console.info('date: ', str);

EXTJS Ext.util.Format.date Automatic date conversion

I have a date field which contains data coming in from the database as 2015/07/31 13:01:53.180z.
Datetime is stored in UTC on database.
My code looks like this:
var startDateTime = Ext.util.Format.date(StartDateTime, 'm/d/y g:i:s A');
But the output I get is the conversion of UTC to IST(Indian).I checked on Chrome,Mozilla and IE.
I got same output all the time
Does ExtJs does this? Because I haven't wrriten any method for conversion.
I use ExtJs 4.1.1
I would appreciate any help on this.
Timezone is appended in the string->JS Date conversion.
To parse the date from database without timezone conversion you should use the Ext.Date.parse explicitly, not automatically through model field type 'date' or simply JS constructor new Date().
For example:
var db_date = '2015/07/31 13:01:53.180z',
js_date = Ext.Date.parse(db_date.substring(0,db_date.length-5), 'Y/m/d H:i:s'),
date_to_show = Ext.util.Format.date(js_date, 'm/d/y g:i:s A');
Obviously "substring" must be replaced by something better, for example you could format db date (cutting timezone part) in the web service serialization.
If you achieve to clean the date string in the web service you can also add "dateFormat" attribute to model fields to parse date correctly into models.

.NET WebService JSON date in ISO-8601 format

I am calling a .net asmx webservice that returns a number of fields. One of the fields in a date. The date is in the format of: "effective_date":"\/Date(978411600000)\/"
According to this SO question: How do I format a Microsoft JSON date? it would be better if the date returned was in ISO 8601 format, this way JavaScript would be able to interpret it as a date.
Currently I use the following javascript: new Date(d.effective_date) and I get the message Invalid Date. According to the linked SO question I should be able to do this if I can get the web service to pass the date in ISO format rather than in \/Date(978411600000)\/ format.
My question is, how do I get the webservice to return the date in ISO 8601 format?
Note:
I'm aware that I can use this (per the answer from the linked question): var date = new Date(parseInt(d.effective_date.substr(6)));, however it is mentioned in a comment that Incoming date values should be formatted in ISO-8601, so I'm wondering how to get the incoming date from the web service to be in this ISO format.
You may use:
var date = new Date(d.effective_date);
date.toISOString(); // ISO-8601 formatted string
JSFiddle: http://jsfiddle.net/nanndoj/gjtkvrsy/

node.js cannot parse ISOString date?

We store every date data in ISO format using new Date().toISOString().
I tried to convert this ISO formatted date into Date object in node.js but I get Invalid Date response.
date string is isoDate = 2014-07-09T14:00:00.000Z
and I did console.log on Date.parse(isoDate); and new Date(isoDate);
but each returns NaN and Invalid Date.
I checked if the date string contains any invisible wrong character but they are fine and can be converted on browser console.
does this mean I need to convert the string manually and create Date object with parsed string?
Thanks for reading.
Try using moment library. It has a lot of functionality to work with dates and can easily be used both on client and server side. Calling moment("2014-07-09T14:00:00.000Z").toDate() would convert your string to a Date JavaScript Object, using this library.
I am posting this answer just in case somebody experience this like I did.
What happened to me is I thought I was sending an ISOString from the browser
{
startDate: date.startDate
}
which in fact I was sending a moment instance as parameter
When I checked in the network inspector I found out that the data being sent is in ISO format - yes, but it is enclosed in double quote ""
{
startDate: "2016-12-31T16:00:00.000Z"
}
it should not be enclosed in double qoutes and should look like this
{
startDate: 2016-12-31T16:00:00.000Z
}
what worked for me is to parse the moment to iso string
{
startDate: date.startDate.toISOString()
}

asp.net mvc datetime dynamic validation by clients date format

I have a Custom Attribute for DateTime validation with given dateformat and also javascript validator which are provide me both client side and server side validation. But now I should change my datetime validation so that it would be performed according clients local DateTime format and I do not know how.
I couldn't find anything that help me.
So please advise me how can I implement at least client side DateTime validation or how can I get client's date format by javascript.
If you can determine the locale of your user, you can use .Net globalization classes to assist with server-side parsing of date time strings. For example:
// Parsed as January 4th
var dt1 = DateTime.Parse("1/4/2013", new CultureInfo("en-US"));
// Parsed as April 1st
var dt2 = DateTime.Parse("1/4/2013", new CultureInfo("en-GB"));
But the best thing to do is avoid this entirely. In your JavaScript code, get the value back as an ISO8601 string - which is culture invariant. Native browser support for this varies. The built-in functions work in IE9+.
// This returns an ISO formatted date, in UTC.
var s = yourDate.ToISOString();
One way to get full browser support, and get an ISO date without converting to UTC, is to use the moment.js library, where ISO8601 is the default format:
// This returns an ISO formatted date, with the user's local offset.
var s = moment(yourDate).format();
// This returns an ISO formatted date, in UTC.
var s = moment(yourDate).utc().format();
When you send these values to the server, you can parse them in your .Net code without concern for culture. The format is already culture invariant. To prevent the server's time zone from interfering, you should parse them as a DateTimeOffset:
// assuming this is an ISO value you got from the client:
var s = "2013-04-20T09:00:00-07:00";
// simply parse it
var dto = DateTimeOffset.Parse(s);
// if you don't care about the offset at this point:
var dt = dto.DateTime;
Of course, if you want to fail gracefully, you can do this instead:
DateTimeOffset dto;
var isValid = DateTimeOffset.TryParse(s, out dto);

Categories

Resources