将大纪元时间转换为具有特定时区的人类可读时间
Convert Epoch time to human readable with specific timezone
要将 epoch dateTime 转换为人类可读的,使用简单的 new date(1495159447834)
就足够了。
我现在遇到的问题是,对于我的混合应用程序,如果用户在他的 phone 日期时间设置中将时区设置为 GMT +12:00,即人类可读的日期时间将与我希望用户拥有的不同,我希望 him/her 遵循服务器时区。
因此,我如何将纪元编号转换为人类可读格式的特定给定时区。
我试过这样的例子:
var test= new Date('1495159447834 GMT+0800').toString();
它 returns 我的日期无效。
如果可能的话,我希望没有任何库。我已经查看了这里的答案,我相信我找不到任何我正在寻找的答案。如果有任何以前回答过的相同主题的问题,请告诉我,我会关闭这个问题!
将初始日期设置为纪元并添加 UTC 单位。假设您有一个以秒为单位存储的 UTC 纪元变量。 1234567890 怎么样。要将其转换为当地时区的正确日期:
var utcSeconds = 1234567890;
var d = new Date(0); // The 0 there is the key, which sets the date to
the epoch
d.setUTCSeconds(utcSeconds);
或者你可以使用 momentjs
moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')
或者你也可以这样使用
var dateVal ="/Date(1342709595000)/";
var date = new Date(parseFloat(dateVal.substr(6)));
document.write(
(date.getMonth() + 1) + "/" +
date.getDate() + "/" +
date.getFullYear() + " " +
date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds()
);
您可以使用偏移量将当前日期时间转换为特定时区。
function convertEpochToSpecificTimezone(timeEpoch, offset){
var d = new Date(timeEpoch);
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); //This converts to UTC 00:00
var nd = new Date(utc + (3600000*offset));
return nd.toLocaleString();
}
// convertEpochToSpecificTimezone(1495159447834, +3)
偏移量将是您的特定时区。示例:GMT +03:00,您的偏移量为 +3。如果 GMT -10:00,偏移量为 -10
有多种方法可以在 Epoch 和人类可读格式之间进行转换
//Convert epoch to human readable date
var myDate = new Date( 1533132667*1000);
document.write(myDate.toGMTString()+"<hr>"+myDate.toLocaleString());
//这将 return 2018 年 8 月 1 日星期三 14:11:07 GMT
//Convert human readable dates to epoch
var myDate = new Date("Wed Aug 01 2018 14:11:07 GMT");
var myEpoch = myDate.getTime()/1000.0;
// 这将 return 1533132667
供参考:https://www.epochconverter.com/programming/#javascript
编辑#
添加了一个 JSFiddle here
这是旧的,但我是这样做的:
function formatDate(date, includeTime) {
const dateTimeFormat = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZone: 'America/Los_Angeles',
timeZoneName: 'short',
});
const [
{ value: month },
,
{ value: day },
,
{ value: year },
,
{ value: hour },
,
{ value: minute },
,
{ value: dayPeriod },
,
{ value: timeZoneName },
] = dateTimeFormat.formatToParts(date);
if (includeTime) {
return `${day} ${month} ${year} • ${hour}:${minute}${dayPeriod.toLowerCase()} ${timeZoneName}`;
}
return `${day} ${month} ${year}`;
这将输出给定时区的时间。
例如,如果我有纪元时间(unix 时间戳)并且我在阿根廷,则时间应显示为 6 月 2 日的 03:45 GMT -3,但是使用此代码,它将显示为洛杉矶应该显示的时间。
我的要求是显示洛杉矶时区的时间,即使我从阿根廷访问该页面也是如此。
要将 epoch dateTime 转换为人类可读的,使用简单的 new date(1495159447834)
就足够了。
我现在遇到的问题是,对于我的混合应用程序,如果用户在他的 phone 日期时间设置中将时区设置为 GMT +12:00,即人类可读的日期时间将与我希望用户拥有的不同,我希望 him/her 遵循服务器时区。
因此,我如何将纪元编号转换为人类可读格式的特定给定时区。
我试过这样的例子:
var test= new Date('1495159447834 GMT+0800').toString();
它 returns 我的日期无效。
如果可能的话,我希望没有任何库。我已经查看了这里的答案,我相信我找不到任何我正在寻找的答案。如果有任何以前回答过的相同主题的问题,请告诉我,我会关闭这个问题!
将初始日期设置为纪元并添加 UTC 单位。假设您有一个以秒为单位存储的 UTC 纪元变量。 1234567890 怎么样。要将其转换为当地时区的正确日期:
var utcSeconds = 1234567890;
var d = new Date(0); // The 0 there is the key, which sets the date to
the epoch
d.setUTCSeconds(utcSeconds);
或者你可以使用 momentjs
moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')
或者你也可以这样使用
var dateVal ="/Date(1342709595000)/";
var date = new Date(parseFloat(dateVal.substr(6)));
document.write(
(date.getMonth() + 1) + "/" +
date.getDate() + "/" +
date.getFullYear() + " " +
date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds()
);
您可以使用偏移量将当前日期时间转换为特定时区。
function convertEpochToSpecificTimezone(timeEpoch, offset){
var d = new Date(timeEpoch);
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); //This converts to UTC 00:00
var nd = new Date(utc + (3600000*offset));
return nd.toLocaleString();
}
// convertEpochToSpecificTimezone(1495159447834, +3)
偏移量将是您的特定时区。示例:GMT +03:00,您的偏移量为 +3。如果 GMT -10:00,偏移量为 -10
有多种方法可以在 Epoch 和人类可读格式之间进行转换
//Convert epoch to human readable date
var myDate = new Date( 1533132667*1000);
document.write(myDate.toGMTString()+"<hr>"+myDate.toLocaleString());
//这将 return 2018 年 8 月 1 日星期三 14:11:07 GMT
//Convert human readable dates to epoch
var myDate = new Date("Wed Aug 01 2018 14:11:07 GMT");
var myEpoch = myDate.getTime()/1000.0;
// 这将 return 1533132667
供参考:https://www.epochconverter.com/programming/#javascript
编辑# 添加了一个 JSFiddle here
这是旧的,但我是这样做的:
function formatDate(date, includeTime) {
const dateTimeFormat = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZone: 'America/Los_Angeles',
timeZoneName: 'short',
});
const [
{ value: month },
,
{ value: day },
,
{ value: year },
,
{ value: hour },
,
{ value: minute },
,
{ value: dayPeriod },
,
{ value: timeZoneName },
] = dateTimeFormat.formatToParts(date);
if (includeTime) {
return `${day} ${month} ${year} • ${hour}:${minute}${dayPeriod.toLowerCase()} ${timeZoneName}`;
}
return `${day} ${month} ${year}`;
这将输出给定时区的时间。
例如,如果我有纪元时间(unix 时间戳)并且我在阿根廷,则时间应显示为 6 月 2 日的 03:45 GMT -3,但是使用此代码,它将显示为洛杉矶应该显示的时间。 我的要求是显示洛杉矶时区的时间,即使我从阿根廷访问该页面也是如此。