Commonly Used JavaScript Date Objects

Site: Philippine Science High School - MC - Knowledge Hub
Course: SY25.CS3.Web Development
Book: Commonly Used JavaScript Date Objects
Printed by: , Guest user
Date: Monday, 12 January 2026, 10:15 AM

1. Introduction

         After completing this module, you are expected to use some of the Date Objects to manipulate data

          This module will also discuss the Date() object. A lot of websites display time and dates. Examples are countdown websites and websites that display the time that you have spent on that website. Have you ever heard of Nyan Cat? Check it out! It gives a time on how long you have stayed on their website. Another example is the Bitcoin Halving Countdown in coinmarketcap.com. It gives a countdown on how much time remains until the next reduction of bitcoin block rewards. The Date() object in JavaScript allows programmers to implement these time features in websites.

2. The Date() Object

          The Date() object allows you to create dates using JavaScript. To create a new date, refer to the code below: 

var d = new Date();

          When you print the value of d, the date is printed in the following format:

Mon Jan 04 2021 11:21:19 GMT+0800 (China Standard Time)

          This is a string, and the time zone is based on your browser’s time zone. The date and time displayed is the time at the moment that your code was run.

          Dates are stored in JavaScript as milliseconds. The starting time is January 1, 1970, 00:00:00 UTC (Universal Standard Time).

          When there are no arguments passed to the Date()object, the date given is the date and time at the given moment that the code was run. If a specific date needs to be set, the Date() object accepts multiple arguments for setting that specific date. You can set a specific data using these arguments. The format is as follows:

var d = new Date(year, month, day, hour, minute, second);

Example:

var d = new Date(2021, 0, 04, 10, 3, 59);

Output: Mon Jan 04 2021 10:03:59 GMT+0800 (China Standard Time)


          The minute, second, hour, and day arguments can be eliminated when specifying dates. When specifying dates, you need at least two arguments, the year and the month. When only a single number x is provided as an argument, that number is considered as milliseconds and the date created will be the date at x milliseconds from time zero. Recall that time zero for JavaScript is January 1, 1970, 00:00:00 UTC.



2.1. An example of using the Date() object for a programming problem

San Francisco, California is -16 hours from our time zone (current time minus 16 hours). Create a program that gives the current time of San Francisco. The code below gives a solution for this problem. Take note that the time zone in the output is based on the time zone of the computer where the code is run and it is not the time zone of San Francisco per se. This is because the only basis for the time of San Francisco is the time difference and the resulting computed value.


Sample user’s current time: Wed Jan 06 2021 11:15:43 GMT+0800 (China Standard Time) Sample Output: Tue Jan 05 2021 19:15:43 GMT+0800 (China Standard Time)

3. Summary and References


Summary

  • The Number object in JavaScript allows programmers to treat primitive values as objects that have their own properties and methods. 

References

JavaScript - The Number Object. (n.d.). RxJS, ggplot2, Python Data Persistence, Caffe2, PyBrain, Python Data Access, H2O, Colab, Theano, Flutter, KNime, Mean.js, Weka, Solidity’ https:// www.tutorialspoint.com/javascript/javascript_number_object.htm

Number. (2020, December 22). Number. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/ JavaScript/Reference/Global_Objects/Number

JavaScript Number() Function. (n.d.). W3Schools Online Web Tutorials. https://www.w3schools.com/jsref/ jsref_number.asp 

JavaScript Set Date Methods. (n.d.). W3Schools Online We Web Tutorials. https://www.w3schools.com/js/ js_date_methods_set.asp