Edo
Edo

Reputation: 85

Get Local timezone from specific zone

I currently use Luxon with the following code:

this.now = DateTime.local();

However, I'd like to get the current time from another timezone like 'Europe/London'. Is this possible in Luxon?

Something like this:

this.now = DateTime.local('Europe/London');

Anyone knows how to do this?

Upvotes: 0

Views: 563

Answers (1)

VincenzoC
VincenzoC

Reputation: 31482

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.

Upvotes: 3

Related Questions