// JavaScript dates don't like hyphens!var rectifiedDateText = dateText.replace(/-/g, "/");var d = new Date(rectifiedDateText);
// Using a predefined mask from date.format.js.var convertedDate = dateFormat(d, 'isoUtcDateTime');
var date = new Date();var now_utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(),date.getUTCDate(), date.getUTCHours(),date.getUTCMinutes(), date.getUTCSeconds());
console.log(new Date(now_utc));console.log(date.toISOString());
function ParseDateForSave(dateValue) {// create a new date objectvar newDate = new Date(parseInt(dateValue.substr(6)));
// return the UTC version of the datereturn newDate.toISOString();}
var now = new Date(); // Fri Feb 20 2015 19:29:31 GMT+0530 (India Standard Time)var isoDate = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString();//OUTPUT : 2015-02-20T19:29:31.238Z
转换为ISO,更改日期/时间(日期/时间将更改)
isoDate = new Date(now).toISOString();//OUTPUT : 2015-02-20T13:59:31.238Z
var year = getFullYear('date as text');var month = getMonth('date as text');var dayOfMonth = getDate('date as text');
var date = new Date(year, month, dayOfMonth);
var offsetInMs = ((date.getTimezoneOffset() * 60) // Seconds* 1000); // Milliseconds
var utcDate = new Date(date.getTime + offsetInMs);
var mydate;//假设这是我想要公开的日期对象var UTCDate=new Date(mydate);/*创建日期对象的副本。只有当您出于某种原因需要原始本地日期时才需要*/UTCDate.setTime(UTCDate.getTime()+UTCDate.getTimezoneOffset()*60*1000);
上面的代码片段基本上是根据时区添加/减去浏览器添加/减去的时间。
例如,如果我在EST(GMT-5)并且我的服务返回日期时间对象=Wed Aug 17 2016 00:00:00 GMT-0500我的浏览器会自动减去时区偏移量(5hrs)以获取我的本地时间。所以如果我尝试获取我获得的时间Wed Aug 16 2016 19:00:00 GMT-0500。这会导致很多问题。那里有很多库肯定会让这更容易,但我想分享纯JS方法。
var userdate = new Date("2009-1-1T8:00:00Z");var timezone = userdate.getTimezoneOffset();var serverdate = new Date(userdate.setMinutes(userdate.getMinutes()+parseInt(timezone)));
//First i had a string called stringDateVar that i needed to convert to Datevar newDate = new Date(stringDateVar)
//output: 2019-01-07T04:00:00.000Z//I needed it 2019-01-07T00:00:00.000Z because i had other logic that was dependent on that
var correctDate = new Date(newDate.setUTCHours(0))
//This will output 2019-01-07T00:00:00.000Z on everything which allows scalability
export function convertLocalDateToUTCIgnoringTimezone(date: Date) {const timestamp = Date.UTC(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds(),date.getMilliseconds(),);
return new Date(timestamp);}
export function convertUTCToLocalDateIgnoringTimezone(utcDate: Date) {return new Date(utcDate.getUTCFullYear(),utcDate.getUTCMonth(),utcDate.getUTCDate(),utcDate.getUTCHours(),utcDate.getUTCMinutes(),utcDate.getUTCSeconds(),utcDate.getUTCMilliseconds(),);}
然后,我保存/读取此日期如下:
function saveTaskDate(localDate: Date) {// I convert your local calendar date so it looks like you've picked it being in UTC somewhere around Londonconst utcDate = convertLocalDateToUTCIgnoringTimezone(localDate);api.saveTaskDate(utcDate);}
function readTaskDate(taskUtcDate: Date) {// I convert this UTC date to 'look in your local timezone' as if you were now in UTC somewhere around londonconst localDateWithSameDayAsUTC = convertUTCToLocalDateIgnoringTimezone(taskUtcDate);
// this date will have the same calendar day as the one you've picked previously// no matter where you were saving it and where you are now}
let date = new Date(YOUR_DATE).toISOString()
// It would give the date in format "2020-06-16T12:30:00.000Z" where Part before T is date in YYYY-MM-DD format, part after T is time in format HH:MM:SS and Z stands for UTC - Zero hour offset
const now = new Date();const year = now.getUTCFullYear();const month = now.getUTCMonth();const day = now.getUTCDate();const hour = now.getUTCHours();
const tomorrowUTC= new Date();tomorrowUTC.setDate(day + 1); // +1 because my logic is to get "tomorrow"tomorrowUTC.setYear(year);tomorrowUTC.setMonth(month);tomorrowUTC.Hours(hour);
// then use the tomorrowUTC for to display/format it// tomorrowUTC is a "Date" and not a string.
你可以这样做:
We will delete your account at ${format(tomorrowUTC, 'EEEE do MMMM hh:mmaaa')} UTC
import { zonedTimeToUtc } from 'date-fns-tz';
const dateBrazil = new Date() // I'm in Brazil, you should have or get the user timezone.const dateUtc = zonedTimeToUtc(dateBrazil, 'America/Sao_Paulo')
if (!Date.prototype.toUTC){Date.prototype.toUTC = function(){var utcOffset = new Date().getTimezoneOffset();var utcNow = new Date().addMinutes(utcOffset);return utcNow;};}