How to work with Date and time in Javascript ??

  • A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.
  • This date and time is the same as the UNIX epoch.


                              Date Format and timezone conversion:

     "There are a number of methods available to obtain a date in various formats, as well as to perform time zone conversions. Particularly useful are the functions that output the date and time in Coordinated Universal Time (UTC), the global standard time defined by the World Time Standard. (This time is historically known as Greenwich Mean Time," 


                                 DATE OBJECT

  •  The Date object is a built-in object in JavaScript that stores the date and time. It provides a number of built-in methods for formatting and managing that data.
  • By default, a new Date instance without arguments provided creates an object corresponding to the current date and time. This will be created according to the current computer’s system settings..

<script>
const getdate = new Date()
console.log(getdate);
</script>

Output:
Thu Jun 11 2020 20:22:52 GMT+0545 (Nepal Time)



Day of the WeekMonthDayYearHourMinuteSecondTimezone
thuJun112020202252GMT+0000 (UTC)


  • JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970. We can get the timestamp with the getTime() method.

<script>
const getdate = new Date()
console.log(getdate.getTime())
</script


Output:
1591886587919


Retrieving the Date with get()

Once we have a date, we can access all the components of the date with various built-in methods. The methods will return each part of the date relative to the local timezone. Each of these methods starts with get, and will return the relative number. Below is a detailed table of the get methods of the Date object.


Date/TimeMethodRangeExample
YeargetFullYear()YYYY1970
MonthgetMonth()0-110 = January
Day (of the month)getDate()1-311 = 1st of the month
Day (of the week)getDay()0-60 = Sunday
HourgetHours()0-230 = midnight
MinutegetMinutes()0-59
SecondgetSeconds()0-59
MillisecondgetMilliseconds()0-999
TimestampgetTime()Milliseconds since Epoch time
<script>
const getdate = new Date()
console.log(getdate.getTime())
console.log(getdate.getMonth())
console.log(getdate.getDay())
console.log(getdate.getHours())
console.log(getdate.getMinutes())
console.log(getdate.getSeconds())
</script>



Output:
1591886844704 date.html: 5 date.html: 4 date.html: 20 date.html: 32 date.html: 24


The built-in Date methods that begin with get allow us to access date components that return the number associated with what we are retrieving from the instantiated object.


Date Methods with UTC

The get methods discussed above retrieve the date components based on the user’s local timezone settings. For increased control over the dates and times, you can use the getUTC methods, which are exactly the same as the get methods, except they calculate the time based on the UTC (Coordinated Universal Time) standard. Below is a table of the UTC methods for the JavaScript Date object.

Date/TimeMethodRangeExample
YeargetUTCFullYear()YYYY1970
MonthgetUTCMonth()0-110 = January
Day (of the month)getUTCDate()1-311 = 1st of the month
Day (of the week)getUTCDay()0-60 = Sunday
HourgetUTCHours()0-230 = midnight
MinutegetUTCMinutes()0-59
SecondgetUTCSeconds()0-59
MillisecondgetUTCMilliseconds()0-999

To test the difference between local and UTC get methods, we can run the following code.

UTC.js
// Assign current time to a variable
const now = new Date();

// Print local and UTC timezones
console.log(now.getHours());
console.log(now.getUTCHours());

Running this code will print out the current hour, and the hour of the UTC timezone. If you are currently in the UTC timezone the numbers that are output from running the program above will be the same.

UTC is useful in that it provides an international time standard reference and can therefore keep your code consistent across timezones if that is applicable to what you are developing.


Modifying the Date with set

For all the get methods that we learned about above, there is a corresponding set method. Where get is used to retrieve a specific component from a date, set is used to modify components of a date. Below is a detailed chart of the set methods of the Date object.

Date/TimeMethodRangeExample
YearsetFullYear()YYYY1970
MonthsetMonth()0-110 = January
Day (of the month)setDate()1-311 = 1st of the month
Day (of the week)setDay()0-60 = Sunday
HoursetHours()0-230 = midnight
MinutesetMinutes()0-59
SecondsetSeconds()0-59
MillisecondsetMilliseconds()0-999
TimestampsetTime()Milliseconds since Epoch time

We can use these set methods to modify one, more, or all of the components of a date. 

toLocateTimeString():

Return the time portion of a Date object as a string, using locale conventions:

var d = new Date();
var n = d.toLocaleTimeString();
const d = new Date();
const n = d.toLocaleTimeString();
console.log(n) // test yourself !!!!

Comments

Popular posts from this blog

JavaScript — Double Equals vs. Triple Equals - weird things on JavaScript

06_Understanding Execution Context and Execution Stack in Javascript

Creating and inserting element- part-2