39
loading...
This website collects cookies to deliver better user experience
2021/06/10 02:20:50
in UTC. Then, the standard notation for this date will be 2021-06-10T02:20:50+00:00
.Date
object that represents a single moment in time. You can create a Date object in different ways:let date;
date = new Date(); // Get the current date
date = new Date("2021-06-10T02:20:50+00:00"); // An object representation of given string date
date = new Date(new Date()); // Creates an object representation from another one
Date
object as follows:let stringInput = "2021-06-10T02:20:50+00:00";
let timeZone = "America/Los_Angeles";
const dateObject = new Date(stringInput).toLocaleString("en-US", {
timeZone,
});
console.log(dateObject); // Prints: 6/9/2021, 7:20:50 PM
toLocaleString
method returns a string with a language-sensitive representation of the Date
object. At the same time, this method supports optional arguments where you can configure the time zone. Find more information about this method here.Luxon is a powerful, modern, and friendly wrapper for JavaScript dates and times.
DateTime
object. It can be considered a wrapper of the native Date
object along with a timezone, and a local configuration.DateTime
object is as follows.import { DateTime } from "luxon";
let dateTime = DateTime.local();
console.log("Current Date", dateTime.toISO()); // 2021-06-22T21:11:45.638-04:00
toISO()
will return an ISO 8601-compliant string representation of the DateTime
object.DateTime
in a specific time zone.// Create a DateTime in a Specific Timezone
let zone = "America/Denver";
let dateTime = DateTime.fromObject({
zone,
});
console.log("Current Date", dateTime.toISO()); // 2021-06-22T19:11:45.640-06:00
America/Denver
as the time zone.let dateTime = DateTime.fromObject({
'America/Denver',
}).set({
day: 1,
month: 5,
year: 2021,
});
console.log("Custom date", dateTime.toISO()); //2021-05-01T19:11:45.641-06:00
set
method allows overriding specific properties such as year
, month
, day
, etc.DateTime
object, and we need to convert it to a different time zone.let dateTime = DateTime.fromObject({
'America/Denver',
}).set({
day: 1,
month: 5,
year: 2021,
});
// Convert existing date to another Timezone
dateTime = dateTime.setZone("America/La_Paz");
console.log("Custom date, America/La_Paz", dateTime.toISO()); //2021-05-01T21:11:45.641-04:00
Settings
class, instead, comes to the rescue:import { Settings } from "luxon";
// Configure the time zone
Settings.defaultZoneName = "America/Denver";
console.log(Settings.defaultZoneName); // Reading the configured time zone.
defaultZoneName
can be used as a set
or get
method for the default time zone when you're working with the library.Settings
class contains other methods to configure Luxon's behavior.DateTime
object again, it will take the configured time zone by default.dateTime = DateTime.local();
console.log("Configured defaultZoneName", dateTime.toISO()); //2021-06-22T19:21:54.362-06:00
America/Denver
.DateTime
objects.DateTime
object:const timeZone = "America/Not_Defined_TZ";
const myDateTime = DateTime.local().setZone(timeZone);
console.log("timeZone valid", myDateTime.isValid); // Prints 'false'
America/Los_Angeles
.