Javascript date objects are used to work with dates and times. Using the new Date() constructor, we can create a new date object. We can also manually set the year, month, day, hour, minute, second, and millisecond fields using local or UTC or GMT time. This way javascript can represent date and time till year 275755.
Time Definition:
- Local time refers to the timezone your computer is in.
- UTC is synonymous with Greenwich Mean Time (GMT) in practice.
There are four different ways to declare a date or construct a date in javascript. The standard way is to use the New Date() which sets the current date and time depend on the browser’s time zone.
Syntax:
new Date( )
new Date(milliseconds)
new Date(datestring)
new Date(year,month,date[,hour,minute,second,millisecond ])
- Date() – This method requires no argument. This will be set to the current date and time
Example:
<script>
/**curdate date object */
var curdate = new Date();
/**display Date */
document.write("Current Date : " + curdate);
</script>
result:
Current Date : Thu Dec 03 2020 09:36:28 GMT+0800 (Philippine Standard Time)
- Milliseconds – This method accepts single parameter milliseconds, which indicates any numeric value. This argument is taken as the internal numeric representation of the date in milliseconds.
example:
<script>
/**curdate date object */
var curdate = new Date(4500);
/**display Date */
document.write("Current Date : " + curdate);
</script>
Result:
Current Date : Thu Jan 01 1970 08:00:04 GMT+0800 (Philippine Standard Time)
- DateString – This method accepts a single parameter dateString, which indicates any string value. It is a string representation of a date and returns the date string with the day. The formatted result of this method is accepted by the Date.parse() method.
Example:
<script>
/**curdate date object */
var curdate = new Date("December 03, 2020 09:42:00");
/**display Date */
document.write("Current Date : " + curdate);
</script>
Result:
Current Date : Thu Dec 03 2020 09:42:00 GMT+0800 (Philippine Standard Time)
- year,month,date[,hour,minute,second,millisecond] – multiple argument
- year: Integer value which represents the year. This should always specify the year in full, i.e., use 2020, instead of use 20.
- Month: Integer value, which represents the month. The integer values starting from 0 for January to 11 for December.
- Date: Integer value, which represents the date.
- Hour: Integer value, which represents the hour in a 24-hours scale.
- Minute: Integer value, which represents the minute.
- Second: Integer value, which represents the second.
- Millisecond: Integer value, which represents the millisecond.
Example:
<script>
/**curdate date object */
var curdate = new Date(2020, 11, 24, 09, 53, 30, 0);
/**display Date */
document.write("Current Date : " + curdate);
</script>
Result:
Current Date : Thu Dec 24 2020 09:53:30 GMT+0800 (Philippine Standard Time)
Javascript Date Objects properties
- Prototype: Prototype allows us to add properties and methods to an object.
- Date constructor: It defines the function that creates a Date object’s prototype.
I have also listed methods on how to retrieve a date in javascript
Methods | Description |
Date() | It returns the present day’s date and time. |
getDate() | It returns the day for the specified date. |
getDay() | It returns the day of the week for the specified date. |
getFullYear() | It returns the year of the specified date. |
getYear() | This method returns the year in the specified date. |
getHours() | It returns the hour on a specified date. |
getMilliseconds() | It returns the milliseconds on the specified date. |
getMinutes() | It returns the minutes on the specified date. |
getMonth() | It returns the month on the specified date. This also finds the month. |
getSeconds() | This method returns the seconds on the specified date. |
getTime() | This method returns the date in terms of numeric value as milliseconds. |
setDate() | This method sets the day of the month for a specified date. |
setFullYear() | This method sets the full year for a specified date. |
Return Universal time(UTC)
Methods | Description |
getUTCDate() | It returns the day of a month for a specified date. |
getUTCDay() | It returns the day of the week for a specified date. |
getUTCFullYear() | This method returns the year for a specified date. |
getUTCHours() | It returns the hours on a specified date. |
getUTCMilliseconds() | This method returns the milliseconds form for a specified date. |
getUTCMinutes() | This method returns the minutes on a specified date. |
getUTCMonth() | This method returns the month for a specified date. |
Custom JavaScript Date Objects Format(yyyy-MM-dd) problem
Sometimes parsing a string to a Javascript date with the format “yyyy-MM-dd” can sometimes be interpreted as a UTC value rather than a local-time value. Parsing Date with this format will result in an invalid date response.
One of the working tricks would be to separate date, month, and year and concatenate it into one string by which you can fully control what specific format you want to achieve.
Below is a sample code snippet.
Note:
- getMonth() -> January = 0; February = 1 ,March = 2 .
- getDate() -> Get the day of a date
- getFullYear() -> Get the year of a date (2020)
var formatted_Date = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Output:
“11/11/2017”
If you are interested in ASP.NET development Tutorials, you can visit my blog page. I also found this article, which contains all the details you need to know about Javascript date. I hope this helps. Happy Coding!!