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/
Related
I am trying to convert a client date / time string on a form into a JSON date / time string using JavaScript and moment (for a Django REST API back end). Here is what I have so far:
document.getElementById("dt_tm").value =
moment(document.getElementById("inp-st").value, "DD/MM/YYYY HH:mm").toJSON();
Two problems with this:
The date format cannot be hard coded as the client may have a different date format,
moment adjusts the date / time and I don't need it to do that because the back end performs that function (using Django time zones).
So for example:
moment("14/05/2016 18:00", "DD/MM/YYYY HH:mm").toJSON() =
"2016-05-14T17:00:00.000Z"
When what I need is:
"2016-05-14T18:00"
(In this example my time zone is currently GMT+1.)
If you would like toJSON to return the date in a different format, redefine moment.fn.toJSON to that it returns with your custom format instead of the default ISO8601 date format. This is outlined in the documentation.
moment.fn.toJSON = function() {
return this.format("YYYY-MM-DDTHH:mmZ");
};
I receive a string that comes in from SQLserver with the format:
'mm/dd/yyyy' or CONVERT(VARCHAR(10), [ActivityDate], 101)
I need to convert that string value, to an actual date value, but keeping the same date format:
mm/dd/yyyy
I need to format the date is because it comes from SQL server in format '2015-02-18 00:00:00.000' to a page that uses angularJS sort and fileter taken from this example: https://scotch.io/tutorials/sort-and-filter-a-table-using-angular
in my table, I have a date column that uses format mm/dd/yyyy, when I type 12/21/2015 I get nothing from the filter even though there are records with this date. The reason why the filter does not work, is because the date even thouhg it displays as mm/dd/yyyy, still has the fromat from sql. The filter works when I type the date 2015-12-21, but that would be misleading to the user.
Does this makes sense?
For your case, you can use new Date() constructor that implicitly calls Date.parse()
new Date('02/21/1994')
//> Date 1994-02-20T21:00:00.000Z
+1 to Claies, i recommend to use moment.js too
I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!
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);
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);