किसी भी अतिरिक्त libs के बिना आप निम्न करने के लिए नीचे दिए गए उपयोगिता तरीकों का उपयोग कर सकते हैं:
:
var dateFromPicker = getDateFromPicker();
var dateUtc = localAsUtc(dateFromPicker);
var iso = dateUtc.toISOString(); // returns "2016-12-06T00:00:00.000Z"
/**
* <b>WARNING</b>: This method should only be used in conjunction with components that handle local dates (i.e. date pickers).
*
* Returns a local date constructed from a UTC date (shifts the time by the local time zone). E.g.:
* <ul><li>2016-01-01T00:00Z (UTC) -> 2016-01-01T00:00-0100 (CVT)
* <li>2016-01-01T00:00Z (UTC) -> 2016-01-01T00:00+0100 (CET)
* </ul>
* @param date a date in UTC time zone
* @returns {Date} the same date & time in the local time zone
*/
function utcAsLocal(date) {
if (isNotValidDate(date)) {
return null;
}
return new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds()
);
}
/**
* <b>WARNING</b>: This method should only be used in conjunction with components that handle local dates (i.e. date pickers).
*
* Returns a UTC date constructed from a local date (shifts the time by the local time zone). E.g.:
* <ul><li>2016-01-01T00:00-0100 (CVT) -> 2016-01-01T00:00Z (UTC)
* <li>2016-01-01T00:00+0100 (GMT) -> 2016-01-01T00:00Z (UTC)
* </ul>
* @param date a date in UTC time zone
* @returns {Date} the same date & time in the UTC time zone
*/
function localAsUtc(date) {
if (isNotValidDate(date)) {
return null;
}
return new Date(Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
date.getMilliseconds()
));
}
function isValidDate (date) {
return !isNotValidDate(date);
}
function isNotValidDate(date) {
return date == null || isNaN(date.getTime());
}
यहाँ कुछ उदाहरण जो पहले और यूटीसी के बाद समय क्षेत्रों अनुकरण कर रहे हैं
var date;
// simulate a datepicker in (CET)
date = new Date("2016-12-06T00:00:00.000+0100");
date.toISOString(); // "2016-12-05T23:00:00.000Z"
date = localAsUtc(date);
date.toISOString(); // "2016-12-06T00:00:00.000Z" sent to server
// simulate a datepicker in (CVT)
date = new Date("2016-12-06T00:00:00.000-0100");
date.toISOString(); // "2016-12-06T01:00:00.000Z"
date = localAsUtc(date);
date.toISOString(); // "2016-12-06T00:00:00.000Z" sent to server
// setting the datepicker date (CET)
date = new Date("2016-12-06T00:00:00.000Z"); // received from server
date.toISOString(); // "2016-12-06T00:00:00.000Z"
date = utcAsLocal(date); // set datepicker with this date shows (06/12/2016)
date.toISOString(); // "2016-12-05T23:00:00.000Z"
// setting the datepicker date (CVT)
date = new Date("2016-12-06T00:00:00.000Z"); // received from server
date.toISOString(); // "2016-12-06T00:00:00.000Z"
date = utcAsLocal(date); // set datepicker with this date shows (06/12/2016)
date.toISOString(); // "2016-12-06T01:00:00.000Z"
लेकिन उत्तर moment.js का उल्लेख किए बिना पूरा नहीं होगा जो हैंडलिंग को बहुत आसान बनाता है।
उत्तर के लिए धन्यवाद लेकिन कोड केवल हमें बताता है कि आउटपुट को प्रारूपित करने के लिए कैसे। यह नहीं बताता कि समय के प्रारूप को स्थानीय समय से यूटीसी समय में कैसे बदला जाए। – user1599647