Categories
Day.js

Manipulating Dates with Day.js — Calendar Time and Date-Time Difference

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.

Calendar Time

We can get a date in calendar time format with the calendar method that comes with the calendar plugin.

For instance, we can write:

const dayjs = require("dayjs");
const calendar = require("dayjs/plugin/calendar");
dayjs.extend(calendar);

const result = dayjs().calendar();
console.log(result);

We call calendar to get the time of today.

Therefore, if the time is 1:23 pm now, we get ‘Today at 1:23 PM‘ for result .

Date-Time Difference

We can get the date-time difference between 2 date-times with the diff method.

For instance, we can write:

const dayjs = require("dayjs");
const date1 = dayjs("2020-01-25");
const date2 = dayjs("2018-06-05");
const result = date1.diff(date2);
console.log(result);

to get the date difference between January 25, 2020, and June 5, 2018.

Therefore, result is 51757200000 and the number is in milliseconds.

We can get the date-time differences in a different unit.

For instance, we can get the difference in months by writing:

const dayjs = require("dayjs");
const date1 = dayjs("2020-01-25");
const date2 = dayjs("2018-06-05");
const result = date1.diff(date2, 'month');
console.log(result);

We passed in 'month' as the 2nd argument of diff to get the month difference between date1 and date2 .

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.

Categories
Day.js

Manipulating Dates with Day.js — Get the Time Relative to a Date

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.

Get the Time to Now

We can get the string of the relative time to now with the toNow method available with the relativeTime plugin.

For instance, we can write:

const dayjs = require("dayjs");
const relativeTime = require("dayjs/plugin/relativeTime");
dayjs.extend(relativeTime);

const result = dayjs("1999-01-01").toNow();
console.log(result);

to get how long from January 1, 1999 until we reach today’s date.

Therefore, result is 'in 23 years‘ in 2021.

We can also pass in true to toNow to remove the suffix.

And we’ll get '23 years' for result .

Get the Time Until We Reach the Given Time

We can get the string of the relative time to now with the to method available with the relativeTime plugin.

For instance, we can write:

const dayjs = require("dayjs");
const relativeTime = require("dayjs/plugin/relativeTime");
dayjs.extend(relativeTime);

const a = dayjs("2020-01-01");
const result = dayjs("1999-01-01").to(a);
console.log(result);

to get how long from January 1, 1999 until we reach January 1, 2020

Therefore, result is 'in 21 years‘

We can also pass in true to to as its 2nd argument to remove the suffix.

And we’ll get '21 years' for result .

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.

Categories
Day.js

Manipulating Dates with Day.js — Relative Time

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.

Get the Time from Now

We get the time from now with a Day.js date by using the fromNow method available with the relativeTime plugin.

For instance, we can write:

const dayjs = require("dayjs");  
const relativeTime = require("dayjs/plugin/relativeTime");  
dayjs.extend(relativeTime);

const result = dayjs("2000-01-01").fromNow();  
console.log(result);

Then we get '22 years ago‘ as the value of result in year 2021.

We can also pass in true to fromNow to remove the suffix from the returned string.

For instance, we can write:

const dayjs = require("dayjs");  
const relativeTime = require("dayjs/plugin/relativeTime");  
dayjs.extend(relativeTime);

const result = dayjs("2000-01-01").fromNow(true);  
console.log(result);

Then result is '22 years' in year 2021.

Get the Time Relative to a Given Time

We can get the time relative to the given time with the from method.

For instance, we can write:

const dayjs = require("dayjs");  
const relativeTime = require("dayjs/plugin/relativeTime");  
dayjs.extend(relativeTime);

const a = dayjs("2020-01-01");  
const result = dayjs("1999-01-01").from(a);  
console.log(result);

Then we get the time difference between a and January 1, 1999.

Therefore, result is '21 years ago‘ .

We can get the relative time string without the suffix if we pass in true as the 2nd argument of from .

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.

Categories
Day.js

Manipulating Dates with Day.js —Formatting Dates

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.

Formatting Dates

We can format Day.js dates into a date string with the format method.

For instance, we can write:

const dayjs = require("dayjs");  
const result = dayjs().format();  
console.log(result);

to return the ISO-8601 format date string of the current date-time.

We can specify how the date is formatted with a string as the argument of format :

const dayjs = require("dayjs");  
const result = dayjs().format("DD/MM/YYYY");  
console.log(result);

The following format strings can be added to format dates:

  • YY — Two-digit year
  • YYYY — Four-digit year
  • M — month, beginning at 1
  • MM — month, 2-digits
  • MMM — abbreviated month name
  • MMMM — full month name
  • D— day of the month
  • DD — day of the month, 2-digits
  • d — day of the week, with Sunday as 0
  • dd — min name of the day of the week
  • ddd — short name of the day of the week
  • dddd — name of the day of the week
  • H — hour
  • HH — hour, 2-digits
  • h — hour, 12-hour clock
  • hh — hour, 12-hour clock, 2-digits
  • m — minute
  • mm — minute, 2-digits
  • s — second
  • ss — second, 2-digits
  • SSS — millisecond, 3-digits
  • Z — offset from UTC, ±HH:mm
  • ZZ — offset from UTC, ±HHmm
  • A — AM PM
  • a — am pm

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.

Categories
Day.js

Manipulating Dates with Day.js — UTC

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.

Convert a UTC Date-Time to Local Date-Time

To convert a UTC date-time to a locale date-time with Day.js, we can use the local method available with the utc plugin.

For instance, we can write:

const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);

const a = dayjs.utc();
const result = a.local().format();
console.log(result);

to convert date a , which is in UTC, to a local date-time.

We import the utc plugin with:

const utc = require("dayjs/plugin/utc");

We can also call utc with true to change the time zone without changing the current time:

const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);

const result = dayjs("2020-05-03 22:15:01").utc(true).format();
console.log(result);

UTC Offset

To set the UTC offset of a Day.js date, we can use the utcOffset method available with the utc plugin with the offset in minutes as the argument:

const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);

const result = dayjs().utcOffset(480);
console.log(result);

We set the date to 8 hours ahead of UTC by calling utcOffset with 480 minutes.

And we can get the UTC offset of a Day.js date by calling utcOffset with no arguments:

const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);

const result = dayjs().utcOffset();
console.log(result);

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.