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.

Categories
Day.js

Manipulating Dates with Day.js — Get the Start or End of a Unit of 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 Start or End of a Unit of Time

We can get the start of a unit of time with the startOf method.

For instance, we can write:

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

to get the date-time of the first day of the year by calling startOf with 'year' .

We can also call startOf with 'year' , 'month' , 'date' , 'hour' , 'minute' , 'second' , and 'millisecond' respectively.

The month starts with 0 for January just like JavaScript dates.

There’re also shorthands for each argument string.

'y' is shorthand for 'year' .

'M' is shorthand for 'month' .

'D' is shorthand for 'date' .

'd' is shorthand for 'day' , which is the day of the week. It starts at 0 for Sunday, and 6 is Saturday.

'h' is shorthand for 'hour' .

'm' is shorthand for 'minute' .

's' is shorthand for 'second' .

And 'ms' is shorthand for 'millisecond' .

We can replace startOf with endOf and keep the same arguments to get the date-time of the end of a given unit of time.

Conclusion

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