Categories
Day.js

Manipulating Dates with Day.js — Set ISO Day of the Week and Day of the Year

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 ISO Day of the Week of a Date

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

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

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

We import the isoWeek plugin with:

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

And we set the ISO day of the week to Monday by calling isoWeekday with 1.

Set the Day of the Year of a Date

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

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

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

We import the isoWeek plugin with:

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

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

If the day of the year exceeds the number of days in the year, then the year will be increased accordingly.

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 Date of the Month and Day of the Week 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 Date of the Month of a Date

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

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

We set the date of the month of the date to 1 with 1 as the argument of date.

Set the Day of the Week of a Date

To set the day of the week in a Day.js date we can use the day method:

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

We set the day of the week of the date to Monday with 1 as the argument of day.

Day of the week ranges from 0 for Sunday to 6 for Saturday.

Set the Locale-Aware Day of the Week of a Date

To set the locale-aware day of the week in a Day.js date we can use the weekday method available with the weekday plugin:

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

const result = dayjs().weekday(-1);
console.log(result);

We import the weekday plugin with:

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

And we set the locale-aware day of the week of the date to last Monday with -1 as the argument of weekday.

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 Milliseconds, Seconds, Minutes, and Hours 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 Number of Milliseconds of a Date

To set the number of milliseconds in a Day.js date we can use the millisecond method:

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

We set the number of milliseconds of the date to 1 with 1 as the argument of milliseconds .

Set the Number of Seconds of a Date

To set the number of seconds in a Day.js date we can use the millisecond method:

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

We set the number of seconds of the date to 1 with 1 as the argument of second.

Set the Number of Minutes of a Date

To set the number of minutes in a Day.js date we can use the minute method:

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

We set the number of minutes of the date to 1 with 1 as the argument of minute. Minutes range from 0 to 59.

Set the Number of Hours of a Date

To set the number of hours in a Day.js date we can use the hours method:

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

We set the number of hours of the date to 1 with 1 as the argument of hour.

Conclusion

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

Categories
Day.js

Manipulating Dates with Day.js — Get 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.

Get Different Date Values

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

For instance, we write:

const dayjs = require("dayjs");
const result = [
  dayjs().get("year"),
  dayjs().get("month"),
  dayjs().get("date"),
  dayjs().get("hour"),
  dayjs().get("minute"),
  dayjs().get("second"),
  dayjs().get("millisecond")
];

console.log(result);

to get the year, month, date of the month, hour, minute, second, and milliseconds of a date with the get method and the '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' .

Conclusion

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

Categories
Day.js

Manipulating Dates with Day.js — Get Year Values 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.

Get the Date of the Year

To get the day of the year in a Day.js date we can use the dayOfYear method available in the dayOfYear plugin:

const dayjs = require("dayjs");
const dayOfYear = require("dayjs/plugin/dayOfYear");
dayjs.extend(dayOfYear);
const result = dayjs().dayOfYear();
console.log(result);

We import the dayOfYear plugin with:

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

Get the Week of the Year of a Date

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

const dayjs = require("dayjs");
const weekOfYear = require("dayjs/plugin/weekOfYear");
dayjs.extend(weekOfYear);
const result = dayjs().week();
console.log(result);

We import the weekOfYear plugin with:

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

Get the ISO Week of the Year of a Date

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

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

We import the isoWeek plugin with:

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

Get the Month of a Date

To get the ISO week of the year in a Day.js date we can use the month method:

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

Get the Quarter of a Date

To get the ISO week 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();
console.log(result);

We import the quarterOfYear plugin with:

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

Get the Year of a Date

To get the ISO week of the year in a Day.js date we can use the year method:

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

Get the Number of Weeks of the Year

To get the number of weeks of the year in a Day.js date we can use the isoWeeksInYear method available with the isoWeeksInYear and isLeapYear plugins:

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

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

We import the plugins with:

const isoWeeksInYear = require("dayjs/plugin/isoWeeksInYear");
const isLeapYear = require("dayjs/plugin/isLeapYear");

Conclusion

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