I currently have the following code:
我目前有以下代码:
lastLoginDate = "\/Date(1499994230140+0800)\/";
lastLoginDate = moment(lastLoginDate).format("YYYY-MM-DD hh:mm:ss UTC");
lastLoginDate = new Date(lastLoginDate);
This works in chrome. However when ran in IE, new Date returns invalid date. how do i achieve the same output in IE?
这在chrome工作。但是当在IE中运行时,新的日期会返回无效的日期。在IE中如何实现相同的输出?
3 个解决方案
#1
1
resolved my issue by changing format to yyyy/mm/dd instead of yyyy-mm-dd
解决了我的问题,改为yyyyy /mm/dd而不是yyyy-mm-dd。
moment(lastLoginDate).format("YYYY/MM/DD hh:mm:ss UTC");
#2
0
There is no need at all to format and then parse the date again. Just use toDate()
:
根本不需要格式化,然后再次解析日期。只使用迄今为止():
lastLoginDate = moment(lastLoginDate).toDate();
If this does not work, then your lastLoginDate
has a wrong format. You can read more about valid formats here.
如果这不起作用,那么您的lastLoginDate格式错误。您可以在这里阅读更多有效的格式。
#3
0
You can split the string into the Unix time and the timezone offset by using a regex.
您可以使用正则表达式将字符串分割为Unix时间和时区。
This code below is easy to follow.
下面的代码很容易理解。
const dateRegex = /^\/Date\((\d+)([-+]\d{4})\)\/$/;
const dateFormat = 'YYYY-MM-DD hh:mm:ss Z';
let lastLoginDate = "\/Date(1499994230140+0800)\/";
console.log(parseTimestamp(lastLoginDate));
function parseTimestamp(timestamp) {
var groups = dateRegex.exec(timestamp);
var unixTime = Math.floor(parseInt(groups[1], 10) / 1000);
var timezoneOffset = groups[2];
return moment.unix(unixTime).utcOffset(timezoneOffset).format(dateFormat);
}
// Output: 2017-07-14 09:03:50 +08:00
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
#1
1
resolved my issue by changing format to yyyy/mm/dd instead of yyyy-mm-dd
解决了我的问题,改为yyyyy /mm/dd而不是yyyy-mm-dd。
moment(lastLoginDate).format("YYYY/MM/DD hh:mm:ss UTC");
#2
0
There is no need at all to format and then parse the date again. Just use toDate()
:
根本不需要格式化,然后再次解析日期。只使用迄今为止():
lastLoginDate = moment(lastLoginDate).toDate();
If this does not work, then your lastLoginDate
has a wrong format. You can read more about valid formats here.
如果这不起作用,那么您的lastLoginDate格式错误。您可以在这里阅读更多有效的格式。
#3
0
You can split the string into the Unix time and the timezone offset by using a regex.
您可以使用正则表达式将字符串分割为Unix时间和时区。
This code below is easy to follow.
下面的代码很容易理解。
const dateRegex = /^\/Date\((\d+)([-+]\d{4})\)\/$/;
const dateFormat = 'YYYY-MM-DD hh:mm:ss Z';
let lastLoginDate = "\/Date(1499994230140+0800)\/";
console.log(parseTimestamp(lastLoginDate));
function parseTimestamp(timestamp) {
var groups = dateRegex.exec(timestamp);
var unixTime = Math.floor(parseInt(groups[1], 10) / 1000);
var timezoneOffset = groups[2];
return moment.unix(unixTime).utcOffset(timezoneOffset).format(dateFormat);
}
// Output: 2017-07-14 09:03:50 +08:00
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>