当我尝试var d = new Date()
时,我得到的是Thu Sep 28 2017 22:13:25 GMT-0500 (Central Daylight Time)
有什么方法可以将"22:13:25"
和"GMT-0500"
分成两个不同的变量?
答案 0 :(得分:2)
let currentTime = new Date();
let offset = new Date().getTimezoneOffset();
let dateFormat = { hour: 'numeric', minute:'numeric', second: 'numeric', hour12: false };
let value = null;
let timeZone = null;
console.log(currentTime.toLocaleString('en-US', dateFormat));
if (offset < 0) {
value = '+';
timeZone = offset/-60;
} else {
value = '-';
timeZone = offset/60;
}
if (timeZone < 10) {
console.log('Your timezone: GMT' + value + '0' + timeZone + '00');
} else {
console.log('Your timezone: GMT' + value + + timeZone + '00');
}
&#13;
那是
答案 1 :(得分:1)
这是:
var d = new Date()
var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
var gmt = "GMT" + (d.toString().split('GMT')[1]);
console.log(time)
console.log(gmt)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
如果您需要解释 - 请随意提问!