JavaScript provides Date object to work with date & time, including days, months, years, hours, minutes, seconds, and milliseconds. Table of Contents
Use the
Create a date object by specifying different parameters in the
Parameters:
In the following example, a date object is created by passing milliseconds in the
The following example shows
various formats of a date string that can be specified in a
You can use any valid separator in the date string to differentiate date segments.
Specify seven numeric values to create a date object with the specified year, month and optionally date, hours, minutes, seconds and milliseconds.
Date Formats JavaScript supports ISO 8601 date format by default -
Convert Date FormatsUse different Date methods to convert a date from one format to another format, e.g., to Universal Time, GMT, or local time format. The following example demonstrates
To get date string in formats other than the ones listed above, you need to manually form the date string using different date object methods. The following example converts a date string to DD-MM-YYYY format. Use third party JavaScript Date library like datejs.com or momentjs.com to work with Dates extensively in JavaScript. Compare Dates in JavaScriptUse comparison operators to compare two date objects.
The simplest way to convert your date to the yyyy-mm-dd format, is to do this: var date = new Date("Sun May 11,2014"); var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000 )) .toISOString() .split("T")[0]; let today = new Date(); let dd = today. getDate(); let mm = today. getMonth()+1; const yyyy = today. getFullYear(); if(dd<10) { dd=`0${dd}`; } if(mm<10) { mm=`0${mm}`; } today = `${mm}-${dd}-${yyyy}`; console. |