我目前使用带有以下代码的Luxon:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
但是,我想从另一个时区(例如“欧洲/伦敦”)获取当前时间。在卢森堡有可能吗?
类似这样的东西:
this.now = DateTime.local();
有人知道该怎么做吗?
答案 0 :(得分:2)
是的,您可以使用setZone
方法:
将“日期时间”的区域“设置”为指定区域。返回一个新构造的DateTime。
或者您可以使用fromObject
指定zone
属性,如评论中的snickersnack所建议。
这里有一个现场样本:
const DateTime = luxon.DateTime;
const now = DateTime.local().setZone('Europe/London');
console.log( now.toLocaleString(DateTime.DATETIME_FULL) );
// Using fromObject as suggested by snickersnack
const nowObj = DateTime.fromObject({ zone: 'Europe/London' });
console.log( nowObj.toLocaleString(DateTime.DATETIME_FULL) );
<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>
另请参见手册的Creating DateTimes in a zone部分。