简体   繁体   English

使用特定时区将纪元时间转换为人类可读的时间

[英]Convert Epoch time to human readable with specific timezone

To convert epoch dateTime to human readable , using a simple new date(1495159447834) will suffice.要将 epoch dateTime 转换为人类可读的,使用一个简单的new date(1495159447834)就足够了。

The problem I'm encountering now is that for my hybrid application, if the user set the time-zone in his phone date time setting to lets say GMT +12:00 ,the human readable dateTime will be different from what I would want the user to have and I would want him/her to follow the server timezone.我现在遇到的问题是,对于我的混合应用程序,如果用户在他的手机日期时间设置中将时区设置为 GMT +12:00 ,那么人类可读的 dateTime 将与我想要的不同用户拥有,我希望他/她遵循服务器时区。

Thus , how would I convert the epoch number to a specific given timezone in a human readable format.因此,我将如何以人类可读的格式将纪元数转换为特定的给定时区。

I have tried example like:我试过这样的例子:

var test= new Date('1495159447834 GMT+0800').toString();

and it returns me an Invalid Date.它返回一个无效日期。

If possible, I would want this without any libraries.如果可能,我希望没有任何库。 I have looked through the answers here and I believe that I could not find any answers I'm looking for.我已经浏览了这里的答案,我相信我找不到任何我正在寻找的答案。 If there is any previously answered question with the same topic, do let me know and I will close this question!如果以前有任何与同一主题有关的问题,请告诉我,我将关闭此问题!

You can use offset to convert your current datetime to a specific timezone.您可以使用偏移量将当前日期时间转换为特定时区。

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)

The offset will be your specific timezone.偏移量将是您的特定时区。 Example: GMT +03:00, your offset is +3.示例:GMT +03:00,您的偏移量为 +3。 If GMT -10:00, offset is -10如果 GMT -10:00,偏移量为 -10

there are number of ways to convert between Epoch and Human readable format有多种方法可以在 Epoch 和人类可读格式之间进行转换

//Convert epoch to human readable date
var myDate = new Date( 1533132667*1000);
document.write(myDate.toGMTString()+"<hr>"+myDate.toLocaleString());

//this will return Wed, 01 Aug 2018 14:11:07 GMT //这将返回格林威治标准时间 2018 年 8 月 1 日星期三 14:11:07

  //Convert human readable dates to epoch
var myDate = new Date("Wed Aug 01 2018 14:11:07 GMT"); 
var myEpoch = myDate.getTime()/1000.0;

// this will return 1533132667 // 这将返回 1533132667

For Reference: https://www.epochconverter.com/programming/#javascript供参考: https : //www.epochconverter.com/programming/#javascript

Edit# added a JSFiddle here编辑#在这里添加了一个 JSFiddle

set the initial date to the epoch and add UTC units.将初始日期设置为纪元并添加 UTC 单位。 Say you have a UTC epoch var stored in seconds.假设您有一个以秒为单位存储的 UTC 纪元变量。 How about 1234567890. To convert that to a proper date in the local time zone: 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);

Or you can use momentjs或者你可以使用momentjs

moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')

or you can use this way或者你可以用这种方式

 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() );

This is old, but I did it this way:这是旧的,但我是这样做的:

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}`;

This will output the time of the given timezone.这将输出给定时区的时间。

For example, if I have an epoch time (unix timestamp) and if I'm in Argentina, the time should be displayed as 03:45 GMT -3 of June 2, but with this code, it will be displayed as the time that should be displayed for Los Angeles.例如,如果我有一个纪元时间(unix 时间戳)并且我在阿根廷,那么时间应该显示为 6 月 2 日的 03:45 GMT -3,但是使用此代码,它将显示为应显示为洛杉矶。 My requirement was to display time in Los Angeles timezone, even if I visit the page from Argentina.我的要求是在洛杉矶时区显示时间,即使我从阿根廷访问该页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM