GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — JavaScript - Get Date Methods

Javascript Tutorial
  • Javascript Basics Tutorial
  • Javascript - Home
  • JavaScript - Roadmap
  • JavaScript - Overview
  • JavaScript - Features
  • JavaScript - Enabling
  • JavaScript - Placement
  • JavaScript - Syntax
  • JavaScript - Hello World
  • JavaScript - Console.log()
  • JavaScript - Comments
  • JavaScript - Variables
  • JavaScript - let Statement
  • JavaScript - Constants
  • JavaScript - Data Types
  • JavaScript - Type Conversions
  • JavaScript - Strict Mode
  • JavaScript - Reserved Keywords
  • JavaScript Operators
  • JavaScript - Operators
  • JavaScript - Arithmetic Operators
  • JavaScript - Comparison Operators
  • JavaScript - Logical Operators
  • JavaScript - Bitwise Operators
  • JavaScript - Assignment Operators
  • JavaScript - Conditional Operators
  • JavaScript - typeof Operator
  • JavaScript - Nullish Coalescing Operator
  • JavaScript - Safe Assignment Operator
  • JavaScript - Delete Operator
  • JavaScript - Comma Operator
  • JavaScript - Grouping Operator
  • JavaScript - Yield Operator
  • JavaScript - Spread Operator
  • JavaScript - Exponentiation Operator
  • JavaScript - Operator Precedence
  • JavaScript Control Flow
  • JavaScript - If...Else
  • JavaScript - While Loop
  • JavaScript - For Loop
  • JavaScript - For...in
  • Javascript - For...of
  • JavaScript - Loop Control
  • JavaScript - Break Statement
  • JavaScript - Continue Statement
  • JavaScript - Switch Case
  • JavaScript - User Defined Iterators
  • JavaScript Functions
  • JavaScript - Functions
  • JavaScript - Function Expressions
  • JavaScript - Function Parameters
  • JavaScript - Default Parameters
  • JavaScript - Function() Constructor
  • JavaScript - Function Hoisting
  • JavaScript - Self-Invoking Functions
  • JavaScript - Arrow Functions
  • JavaScript - Function Invocation
  • JavaScript - Function call()
  • JavaScript - Function apply()
  • JavaScript - Function bind()
  • JavaScript - Closures
  • JavaScript - Variable Scope
  • JavaScript - Global Variables
  • JavaScript - Smart Function Parameters
  • JavaScript Objects
  • JavaScript - Number
  • JavaScript - Boolean
  • JavaScript - Strings
  • JavaScript - Arrays
  • JavaScript - Date
  • JavaScript - DataView
  • JavaScript - Handler
  • JavaScript - Math
  • JavaScript - RegExp
  • JavaScript - Symbol
  • JavaScript - Sets
  • JavaScript - WeakSet
  • JavaScript - Maps
  • JavaScript - WeakMap
  • JavaScript - Iterables
  • JavaScript - Reflect
  • JavaScript - TypedArray
  • JavaScript - Template Literals
  • JavaScript - Tagged Templates
  • Object Oriented JavaScript
  • JavaScript - Objects
  • JavaScript - Classes
  • JavaScript - Object Properties
  • JavaScript - Object Methods
  • JavaScript - Static Methods
  • JavaScript - Display Objects
  • JavaScript - Object Accessors
  • JavaScript - Object Constructors
  • JavaScript - Native Prototypes
  • JavaScript - ES5 Object Methods
  • JavaScript - Encapsulation
  • JavaScript - Inheritance
  • JavaScript - Abstraction
  • JavaScript - Polymorphism
  • JavaScript - Destructuring
  • JavaScript - Destructuring Assignment
  • JavaScript - Object Destructuring
  • JavaScript - Array Destructuring
  • JavaScript - Nested Destructuring
  • JavaScript - Optional Chaining
  • JavaScript - Global Object
  • JavaScript - Mixins
  • JavaScript - Proxies
  • JavaScript Versions
  • JavaScript - History
  • JavaScript - Versions
  • JavaScript - ES5
  • JavaScript - ES6
  • ECMAScript 2016
  • ECMAScript 2017
  • ECMAScript 2018
  • ECMAScript 2019
  • ECMAScript 2020
  • ECMAScript 2021
  • ECMAScript 2022
  • JavaScript Asynchronous
  • JavaScript - Asynchronous
  • JavaScript - Callback Functions
  • JavaScript - Promises
  • JavaScript - Async/Await
  • JavaScript - Microtasks
  • JavaScript - Promisification
  • JavaScript - Promises Chaining
  • JavaScript - Timing Events
  • JavaScript - setTimeout()
  • JavaScript - setInterval()
  • JavaScript Cookies
  • JavaScript - Cookies
  • JavaScript - Cookie Attributes
  • JavaScript - Deleting Cookies
  • JavaScript Browser BOM
  • JavaScript - Browser Object Model
  • JavaScript - Window Object
  • JavaScript - Document Object
  • JavaScript - Screen Object
  • JavaScript - History Object
  • JavaScript - Navigator Object
  • JavaScript - Location Object
  • JavaScript - Console Object
  • JavaScript Web APIs
  • JavaScript - Web API
  • JavaScript - History API
  • JavaScript - Storage API
  • JavaScript - Forms API
  • JavaScript - Worker API
  • JavaScript - Fetch API
  • JavaScript - Geolocation API
  • JavaScript Events
  • JavaScript - Events
  • JavaScript - DOM Events
  • JavaScript - addEventListener()
  • JavaScript - Mouse Events
  • JavaScript - Keyboard Events
  • JavaScript - Form Events
  • JavaScript - Window/Document Events
  • JavaScript - Event Delegation
  • JavaScript - Event Bubbling
  • JavaScript - Event Capturing
  • JavaScript - Custom Events
  • JavaScript Error Handling
  • JavaScript - Error Handling
  • JavaScript - try...catch
  • JavaScript - Debugging
  • JavaScript - Custom Errors
  • JavaScript - Extending Errors
  • JavaScript Important Keywords
  • JavaScript - this Keyword
  • JavaScript - void Keyword
  • JavaScript - new Keyword
  • JavaScript - var Keyword
  • JavaScript HTML DOM
  • JavaScript - HTML DOM
  • JavaScript - DOM Methods & Properties
  • JavaScript - DOM Document
  • JavaScript - DOM Elements
  • JavaScript - DOM Attributes (Attr)
  • JavaScript - DOM Forms
  • JavaScript - Changing HTML
  • JavaScript - Changing CSS
  • JavaScript - DOM Animation
  • JavaScript - DOM Navigation
  • JavaScript - DOM Collections
  • JavaScript - DOM NodeList
  • JavaScript - DOM DOMTokenList
  • JavaScript Advanced Chapters
  • JavaScript - Bubble Sort Algorithm
  • JavaScript - Circular Reference Error
  • JavaScript - Code Testing with Jest
  • JavaScript - CORS Handling
  • JavaScript - Data Analysis
  • JavaScript - Dead Zone
  • JavaScript - Design Patterns
  • JavaScript - Engine and Runtime
  • JavaScript - Execution Context
  • JavaScript - Function Composition
  • JavaScript - Immutability
  • JavaScript - Kaboom.js
  • JavaScript - Lexical Scope
  • JavaScript - Local Storage
  • JavaScript - Memoization
  • JavaScript - Minifying JS
  • JavaScript - Mutability vs Immutability
  • JavaScript - Package Manager
  • JavaScript - Parse S-Expressions
  • JavaScript - Prototypal Inheritance
  • JavaScript - Reactivity
  • JavaScript - Require Function
  • JavaScript - Selection API
  • JavaScript - Session Storage
  • JavaScript - SQL CRUD Operations
  • JavaScript - Supercharged Sorts
  • JavaScript - Temporal Dead Zone
  • JavaScript - Throttling
  • JavaScript - TRPC Library
  • JavaScript - Truthy and Falsy Values
  • JavaScript - Upload Files
  • JavaScript - Date Comparison
  • JavaScript - Recursion
  • JavaScript - Data Structures
  • JavaScript - Base64 Encoding
  • JavaScript - Callback Function
  • JavaScript - Current Date/Time
  • JavaScript - Date Validation
  • JavaScript - Filter Method
  • JavaScript - Generating Colors
  • JavaScript - HTTP Requests
  • JavaScript - Insertion Sort
  • JavaScript - Lazy Loading
  • JavaScript - Linked List
  • JavaScript - Nested Loop
  • JavaScript - Null Checking
  • JavaScript - Get Current URL
  • JavaScript - Graph Algorithms
  • JavaScript - Higher Order Functions
  • JavaScript - Empty String Check
  • JavaScript - Form Handling
  • JavaScript - Functional Programming
  • JavaScript - Parameters vs Arguments
  • JavaScript - Prototype
  • JavaScript - Reactive Programming
  • JavaScript - Reduce Method
  • JavaScript - Rest Operator
  • JavaScript - Short Circuiting
  • JavaScript - Undefined Check
  • JavaScript - Unit Testing
  • JavaScript - Validate URL
  • JavaScript Miscellaneous
  • JavaScript - Ajax
  • JavaScript - Async Iteration
  • JavaScript - Atomics Objects
  • JavaScript - Rest Parameter
  • JavaScript - Page Redirect
  • JavaScript - Dialog Boxes
  • JavaScript - Page Printing
  • JavaScript - Validations
  • JavaScript - Animation
  • JavaScript - Multimedia
  • JavaScript - Image Map
  • JavaScript - Browsers
  • JavaScript - JSON
  • JavaScript - Multiline Strings
  • JavaScript - Date Formats
  • JavaScript - Get Date Methods
  • JavaScript - Set Date Methods
  • JavaScript - Modules
  • JavaScript - Dynamic Imports
  • JavaScript - BigInt
  • JavaScript - Blob
  • JavaScript - Unicode
  • JavaScript - Shallow Copy
  • JavaScript - Call Stack
  • JavaScript - Reference Type
  • JavaScript - IndexedDB
  • JavaScript - Clickjacking Attack
  • JavaScript - Currying
  • JavaScript - Graphics
  • JavaScript - Canvas
  • JavaScript - Debouncing
  • JavaScript - Performance
  • JavaScript - Style Guide
  • JavaScript Useful Resources
  • JavaScript - Questions And Answers
  • JavaScript - Quick Guide
  • JavaScript - Cheatsheet
  • JavaScript - Functions
  • JavaScript - Resources
  • JavaScript - Online Editor
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

JavaScript - Get Date Methods



Previous
Quiz
Next

Get Date Methods

JavaScript utilizes the Date object for date and time manipulation. This object offers an array of methods that facilitate retrieval and modification of date-specific information. Here, we will discuss about the get date methods within JavaScript which fetch different components of the date/time.

Below is a table of the most commonly used get date methods and their corresponding description.

Method Description
getFullYear() This method fetches and presents the comprehensive calendar year by retrieving the current year in local time zone; it returns the full four-digit representation of a local date object.
getMonth() Returns the month (0-11) of the local date object. This method retrieves the current month, with values ranging from 0 (January) to 11 (December). It's useful for displaying and manipulating month-related information.
getDate() The method: 'returns the day component of the current date', a value ranging from 1 to 31. This functionality proves particularly useful when one needs this information extracted from a local date object.
getHours() The function 'getHours()' extracts and returns the local date object's hour component (0-23). This allows you to retrieve the current hour in your local time zone for a variety of time-related applications.
getMinutes() Returns the minutes (0-59) of the local date object. Retrieves the current minute component, ranging from 0 to 59. Useful for displaying and handling time-related data at the minute level.
getSeconds() This returns the seconds ranging from 0 to 59 of the local date object. It provides precision down the seconds for a variety of time-based calculations/displays.
getMilliseconds() Returns the milliseconds (0-999) of the local date object. Retrieves the current millisecond component, allowing for high precision in time-related applications and calculations.
getDay() Returns the index of day of the week starting from 0 which stands for Sunday, all the way up to 6 for Saturday.
getUTCFullYear() Returns the full 4-digit year of the date object in Coordinated Universal Time (UTC). This method retrieves the current year in UTC, providing a standardized representation of the calendar year irrespective of the local time zone.
getUTCMonth() Returns the index of the month ranging from 0(Jan) to 11(Dec) but of the date object in Coordinated Universal Time (UTC).
getUTCDate() Returns the day of the month (1-31) of the date object in Coordinated Universal Time (UTC). Useful for obtaining the day component of the current date in a UTC context.
getUTCHours() Returns the hour (0-23) of the date object in Coordinated Universal Time (UTC). Retrieves the current hour in UTC, allowing for standardized access to the hour component across different time zones.
getUTCMinutes() Returns the minutes (0-59) of the date object in Coordinated Universal Time (UTC). Retrieves the current minute component in UTC, providing standardized minute information for various international time-based applications.
getUTCSeconds() The function fetches the seconds (ranging from 0 to 59) of a date object in Coordinated Universal Time (UTC). It also acquires the current second component in UTC, thereby enabling standardized second information across various time zones.
getUTCMilliseconds() The function returns the milliseconds (0-999) of the date object in Coordinated Universal Time (UTC); it retrieves and offers high precision for standardized time-related calculations and applications: specifically, it provides the current millisecond component in UTC.

Examples

Example 1: Simple demonstration of get date methods

The following example demonstrates the fundamental application of prevalent JavaScript date methods: It instantiates a novel Date object to represent the present date and time; subsequently, it exhibits an array of diverse components - year, month, day; hours, minutes, seconds, milliseconds; along with their corresponding UTC counterparts. The displayed elements encompass not only standard temporal divisions but also supplementary information about weekdays: thus providing comprehensive insight into current temporal dynamics.

<!DOCTYPE html>
<html>
<head>
   <title> Exxample to demonstrate get date methods in JavaScript</title>
</head>
<body>
   <script>
      // Create a new Date object
      const currentDate = new Date();

      function displayResult(methodName, result) {
         const resultDiv = document.createElement('div');
         resultDiv.innerHTML = `${methodName}: ${result}`;
         document.body.appendChild(resultDiv);
      }

      displayResult('getFullYear()', currentDate.getFullYear());
      displayResult('getMonth()', currentDate.getMonth());
      displayResult('getDate()', currentDate.getDate());
      displayResult('getHours()', currentDate.getHours());
      displayResult('getMinutes()', currentDate.getMinutes());
      displayResult('getSeconds()', currentDate.getSeconds());
      displayResult('getMilliseconds()', currentDate.getMilliseconds());
      displayResult('getDay()', currentDate.getDay());
      displayResult('getUTCFullYear()', currentDate.getUTCFullYear());
      displayResult('getUTCMonth()', currentDate.getUTCMonth());
      displayResult('getUTCDate()', currentDate.getUTCDate());
      displayResult('getUTCHours()', currentDate.getUTCHours());
      displayResult('getUTCMinutes()', currentDate.getUTCMinutes());
      displayResult('getUTCSeconds()', currentDate.getUTCSeconds());
      displayResult('getUTCMilliseconds()', currentDate.getUTCMilliseconds());
   </script>
</body>
</html>

Example 2: Comparison of two dates

In this example, the Date constructor creates two specific dates: date1 and date2. The script subsequently compares these dates; it displays their formatted representations, along with a message indicating if date1 is later, earlier or equal to date2.

<!DOCTYPE html>
<html>
<head>
   <title>Comparison of two dates in JavaScript</title>
</head>
<body>
   <script>
      const date1 = new Date(2024, 0, 18);
      const date2 = new Date(2024, 0, 26);

      function displayComparison() {
         const date1Div = document.createElement('div');
         date1Div.innerHTML = `Date 1: ${date1.toDateString()}`;
         document.body.appendChild(date1Div);

         const date2Div = document.createElement('div');
         date2Div.innerHTML = `Date 2: ${date2.toDateString()}`;
         document.body.appendChild(date2Div);

         const resultDiv = document.createElement('div');
         if (date1 > date2) {
            resultDiv.innerHTML = "date 1 is later than date 2";
         } else if (date1 < date2) {
            resultDiv.innerHTML = "date 1 is earlier than date 2";
         } else {
            resultDiv.innerHTML = "Both dates are equal";
         }
         document.body.appendChild(resultDiv);
      }

      displayComparison();
   </script>
</body>
</html>
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.