显示“小时和分钟”适用于除野生动物园之外的所有浏览器。 JavaScript日期 [英] Display "hours and minutes" works on all browsers except safari. JavaScript Date

查看:77
本文介绍了显示“小时和分钟”适用于除野生动物园之外的所有浏览器。 JavaScript日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从对象中以这种格式循环接收日期:2018-08-06 20:45:00

I receive a date from an object in a loop in this format : 2018-08-06 20:45:00

我只想显示 20客户端的时区中的:45(分钟始终为两位数)。我已经在vue.js的方法部分中创建了此方法:

And I want to display only "20:45" (always with two digits for minutes) in the Timezone of the client. I have created this method in the methods part of vue.js :

ihaveadate(dateFromLoop) {
  var d = new Date(dateFromLoop);
  d.setTime(d.getTime() + new Date().getTimezoneOffset() * 60 * 1000);
  var hour = d.getUTCHours();
  var minutes = (d.getMinutes() < 10 ? "0" : "") + d.getMinutes();
  return `${hour}:${minutes}`;
}

除了在Safari浏览器上我有NaN:NaN,似乎工作正常有一个解决这个问题的方法吗?

It seemed to work fine except that on safari I have NaN:NaN, is there a clean solution to this problem ?

读完您的答案后,我就这样了:

After reading ur answers i made it works like that :

var dateFromLoop = '2018-08-06 20:45:00';
var date = Date.parse(dateFromLoop.replace(" ", "T"));
const daty = new Intl.DateTimeFormat({
  hour: "numeric",
  minute: "numeric"
}).format(date);
console.log(daty);

推荐答案

是的,对于 ECMAScript 5 ISO-8601格式支持


或者,日期/时间字符串可能在ISO 8601中rmat。以
为例,可以传递和解析 2011-10-10(日期)或 2011-10-10T14:48:00(日期和
时间)。

Alternatively, the date/time string may be in ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed.

您需要在日期时间中包括 T ,以便使其在Safari中起作用:

you need to include T withing the date time in order to make it work on Safari:

new Date('2014-02-18T15:00:48');

您可以尝试执行以下操作:

you can try and do it like this:

new Date(dateFromLoop.replace(/\s/, 'T');

这篇关于显示“小时和分钟”适用于除野生动物园之外的所有浏览器。 JavaScript日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆