我有一个带有时区的日期/时间,想将其转换为UTC
const date = '2019-04-10T20:30:00Z';
const zone = 'Asia/Kuala_Lumpur';
const utcDate = moment(date).tz(zone).utc().format();
console.log('UTC Date : ', utcDate);
我的日期变量是否在UTC的标准格式中?如何将此时区转换为另一个时区?
答案 0 :(得分:2)
UTC时区用后缀“ Z”表示,因此您需要删除"Z"
并使用moment.tz(..., String)
而不是moment().tz(String)
,因为第一个创建带有时区的时刻,并且秒用于更改现有时刻的时区:
const date = '2019-04-10T20:30:00';
const zone = 'Asia/Kuala_Lumpur';
const utcDate = moment.tz(date, zone).utc().format();
console.log('UTC Date : ', utcDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>
答案 1 :(得分:1)
// keep the sort method unchanged
static <E> void lomuto(final List<? extends E> list,
final Comparator<? super E> comparator) {
sort(list,
comparator,
(List<? extends E> l, Comparator<? super E> c) -> {
final E pivot = l.get(l.size() - 1);
int i = 0;
for (int j = 0; j < l.size() - 1; j++) {
if (c.compare(l.get(j), pivot) <= 0) {
swap(l, j, i++);
}
}
swap(l, l.size() - 1, i);
return i;
});
}
答案 2 :(得分:0)
您可以像下面的代码那样进行操作:
// your inputs
var date = '2019-04-10T20:30:00Z';
var desiredFormate = "MM/DD/YYYY h:mm:ss A"; // must match the input
var zone = 'Asia/Kuala_Lumpur';
// construct a moment object
var m = moment.tz(date , desiredFormate, zone);
// convert it to utc
m.utc();
// format it for output
var s = m.format(fmt) // result: 2017-08-31T08:45:00+06:00
答案 3 :(得分:0)
您可以使用moment.js文档:
moment().format('MMMM Do YYYY, h:mm:ss a'); // April 10th 2019, 3:29:36 pm
moment().format('dddd'); // Wednesday
moment().format("MMM Do YY"); // Apr 10th 19
moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019
moment().format();