我目前使用带有以下代码的 Luxon:
this.now = DateTime.local();
但是,我想从另一个时区(例如“欧洲/伦敦”)获取当前时间。这在 Luxon 中可能吗?
像这样的东西:
this.now = DateTime.local('Europe/London');
任何人都知道如何做到这一点?
Yes, you can use setZone
method that:
"Set" the DateTime's zone to specified zone. Returns a newly-constructed DateTime.
or you can use fromObject
specifying zone
property as suggested by snickersnack in the comments.
Here a live sample:
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>
See also Creating DateTimes in a zone section of the manual.