How to get day in moment.js?

by porter.bins , in category: JavaScript , 2 years ago

How to get day in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by ramon , 2 years ago

@porter.bins You can use format() function with "dddd" as argument to get day in moment.js, code as example:


1
2
3
4
5
import moment from "moment";

const now = moment();
// Output: Friday
console.log(now.format("dddd"));

Member

by cierra , 10 months ago

@porter.bins 

You can get the day of the week using moment.js by calling the .day() method. This method returns the numeric value of the day of the week, with Sunday being 0 and Saturday being 6.


Here is an example:

1
2
3
4
5
const moment = require('moment');

const date = moment();

console.log(date.day()); // outputs the numeric value for the day of the week


You can also format the day of the week using moment.js by calling the .format() method with a format string that includes the day of the week token (d).


Here is an example:

1
2
3
4
5
const moment = require('moment');

const date = moment();

console.log(date.format('dddd')); // outputs the full name of the day of the week


Other format options for the day of the week token (d) include:

  • d: Numeric value of the day of the week (0-6)
  • dd: Zero-padded numeric value of the day of the week (e.g. "01" for Monday)
  • ddd: Abbreviated name of the day of the week (e.g. "Mon")
  • dddd: Full name of the day of the week (e.g. "Monday")