Jquery new date
In jQuery, you can create a new date object using the Date()
constructor. Here are a few ways to do it:
-
Using the
Date()
constructor:var date = new Date();
This will create a new date object with the current date and time.
-
Using the
Date()
constructor with arguments:var date = new Date(year, month, day, hour, minute, second);
This will create a new date object with the specified year, month, day, hour, minute, and second.
For example:
var date = new Date(2022, 5, 12, 14, 30, 0);
This will create a new date object with the year 2022, month 5 (June), day 12, hour 14, minute 30, and second 0.
-
Using the
moment()
function from the Moment.js library:var date = moment();
This will create a new date object with the current date and time.
-
Using the
Date.now()
method:var date = new Date(Date.now());
This will create a new date object with the current date and time.
Note that the Date()
constructor and the moment()
function both return a date object, but the Date.now()
method returns a timestamp (the number of milliseconds since the Unix epoch).
You can also use the parse()
method to create a date object from a string:
var date = new Date("2022-06-12T14:30:00");
This will create a new date object with the specified date and time.
You can also use the parse()
method with a format string:
var date = new Date("2022-06-12T14:30:00", "yyyy-MM-dd'T'HH:mm:ss");
This will create a new date object with the specified date and time, using the specified format string.
It's worth noting that the Date()
constructor and the moment()
function both return a date object, but the Date.now()
method returns a timestamp (the number of milliseconds since the Unix epoch).