Javascript datetime for string and back to datetime?

if you needed to save the current javascript time in a string, what would it look like and could you convert it back to datetime to read javascript?

What I'm trying does not work, given the current xml line Tue Dec 23 12:02:08 EST 2014

var xmlImagePath = $(this).find('pathName').text();

var xmlStartTime = $(this).find('startTime').text();
xmlStartTime = new Date(xmlStartTime);

var fortnightAway = new Date(xmlStartTime);
var numberOfDaysToAdd = 14;
fortnightAway.setDate(fortnightAway.getDate() + numberOfDaysToAdd);


if (fortnightAway < xmlStartTime) {
    alert("here");
}

I do not believe that xmlStartTime = new Date(xmlStartTime);sets xmlStartTime for a datetime object.

Also, What is the correct format for storing datetime in xml so that it is easier to test later?

+4
source share
2 answers

One easy way to serialize dates is to use JSON.stringifyand JSON.parse:

var serialized = JSON.stringify(new Date());

var deserialized = new Date(JSON.parse(serialized));

If you don't have an object JSON, you can do this, which is essentially the same, but with less code embedded:

var iso = (new Date()).toISOString();

var dateObj = new Date(iso);

.toISOString (IE 8 ), polyfill .

+5

moment.js . , , javascript. , , :

http://jsfiddle.net/JamesWClark/9PAFg/

, moment('Tue Dec 23 12:02:08 EST 2014').format() 2014-12-23T11: 02: 08-06: 00, DateTime.

, :

var xmlStartTime = moment($(this).find('startTime').text());
xmlStartTime = new Date(xmlStartTime);
+1

Source: https://habr.com/ru/post/1568659/


All Articles