Get and Set Date Function in JS

Get and Set Date Function in JS

  • new Date (year, month, date, hours, minutes, seconds, ms)

Getting date components

  • getFullYear () =>Get the year (4 digits)
  • getMonth () =>Get the month, from 0 to 11.
  • getDate()=>Get the day of month, from 1 to 31
  • getHours()
  • getMinutes()
  • getSeconds()
  • getMilliseconds()
  • getDay()=>Get the day of week, from 0 (Sunday) to 6 (Saturday).

Example

  • const year = new Date();
  • alert(year.getFullYear());

Setting date components

  • setFullYear(year, [month], [date])
  • setMonth(month, [date])
  • setDate(date)
  • setHours(hour, [min], [sec], [ms])
  • setMinutes(min, [sec], [ms])
  • setSeconds(sec, [ms])
  • setMilliseconds(ms)
  • setTime(milliseconds)

Example

  • const event = new Date();
  • setFullYear(1990);
  • alert(event.getFullYear());
One thought on “Get and Set Date Function in JS

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*