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.

Categories
Day.js

Manipulating Dates with Day.js — Add or Subtract a Given Amount 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.

Add or Subtract a Given Amount of Time

We can add a given amount of time to our Day.js date object with the add method.

For instance, we can write:

const dayjs = require("dayjs");  
const result = dayjs().add(7, "day");  
console.log(result);

to add 7 days to the current date-time.

We call the add method with one of 'year' , 'month' , 'date' , 'hour' , 'minute' , 'second' , and 'millisecond' unit values as the first argument.

The 2nd argument is the value to set.

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 add with subtract and keep the same arguments to subtract a given amount of time.

Conclusion

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

Categories
Day.js

Manipulating Dates with Day.js — Minimum Between Multiple Dates and Set Different Date Values

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.

Minimum Between Multiple Dates

To get the maximum of multiple dates, we can use the min method available with the minMax plugin:

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

const result = dayjs.min(
  dayjs("2020-01-01"),
  dayjs("2018-01-01"),
  dayjs("2019-01-01")
);
console.log(result);

We import the minMax plugin with:

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

And we get the min date from the dates we passed into the min method.

Set Different Date Values

We can set different date values with the set method and the unit of the date part to set as the argument.

For instance, we write:

const dayjs = require("dayjs");
const result = [
  dayjs().set("year", 2020),
  dayjs().set("month", 1),
  dayjs().set("date", 1),
  dayjs().set("hour", 1),
  dayjs().set("minute", 1),
  dayjs().set("second", 1),
  dayjs().set("millisecond", 1)
];
console.log(result);

to set the year, month, date of the month, hour, minute, second, and milliseconds of a date with the set method and the 'year' , 'month' , 'date' , 'hour' , 'minute' , 'second' , and 'millisecond' values respectively as the first argument.

The 2nd argument is the value to set.

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' .

Conclusion

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

Categories
Day.js

Manipulating Dates with Day.js — Set the Year of a Date and Get Max Values Between Multiple 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.

Set the Quarter of the Year of a Date

To set the quarter of the year in a Day.js date we can use the quarter method available with the quarterOfYear plugin:

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

const result = dayjs().quarter(1);
console.log(result);

We import the quarterOfYear plugin with:

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

And we set the quarter of the year to the first quarter by calling quarter with 1.

Set the Year of a Date

To set the year of a date in a Day.js date we can use the year method available with the year plugin:

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

We set the year of the date to 2020 by calling year with 2020.

Maximum Between Multiple Dates

To get the maximum of multiple dates, we can use the max method available with the minMax plugin:

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

const result = dayjs.max(
  dayjs("2020-01-01"),
  dayjs("2018-01-01"),
  dayjs("2019-01-01")
);
console.log(result);

We import the minMax plugin with:

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

And we get the max date from the dates we passed into the max method.

Conclusion

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

Categories
Day.js

Manipulating Dates with Day.js — Set the Week of the Year and Month of 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.

Set the Week of the Year of a Date

To set the week of the year in a Day.js date we can use the weekOfYear method available with the weekOfYear plugin:

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

const result = dayjs().week(1);
console.log(result);

We import the weekOfYear plugin with:

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

And we set the week of the year to the first week by calling week with 1.

Set the ISO Week of the Year of a Date

To set the ISO week of the year in a Day.js date we can use the isoWeek method available with the isoWeek plugin:

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

const result = dayjs().isoWeek(1);
console.log(result);

We import the isoWeek plugin with:

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

And we set the ISO week of the year to the first week by calling isoWeek with 1.

Set the Month of a Date

To set the month of Day.js date we can use the month method:

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

We call month with 1 to set the month the date to February. Month ranges from 0 for January to 11 for December.

Conclusion

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