Skip to content

Latest commit

 

History

History
132 lines (104 loc) · 4.04 KB

date-and-time.md

File metadata and controls

132 lines (104 loc) · 4.04 KB

⚑ Date and Time

JavaScript provides a rich set of built-in functions and objects to handle dates and times.

☴ Overview:

  1. Creating Date Objects
  2. Getting Date and Time Components
  3. Formatting Dates and Times
  4. Time Differences
  5. Time Zones
  6. Setting a Time

✦ Creating Date Objects:

To create a Date object to represent a specific instant in time.

Example:

// Current time
const now = new Date();
console.log(now); // Output: Fri Nov 22 2024 17:02:07 GMT+0530 (India Standard Time) (example)

// Specific date and time
const specificDate = new Date(2024, 10, 22, 16, 59, 0); // Month is 0-indexed (Starts January with 0)
console.log(specificDate); // Output: Fri Nov 22 2024 16:59:00 GMT+0530 (India Standard Time) (example)

✦ Getting Date and Time Components:

To get various components from a Date object.

Example:

// Current time
const now = new Date();

// Various date components
const year = now.getFullYear();
const month = now.getMonth() + 1; // Months are 0-indexed
const date = now.getDate();
const day = now.getDay(); // Day of the week (0-6, 0 is Sunday)
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const milliSeconds = now.getMilliseconds();

console.log(`Date: ${date}/${month}/${year} Time: ${hours}:${minutes}:${seconds}`);
// Output: Date: 22/11/2024 Time: 17:5:46 (example)

✦ Formatting Dates and Times:

To format dates and times using the toLocaleString() and toLocaleTimeString() methods.

Example:

// Current time
const now = new Date();

const formattedDate = now.toLocaleDateString();
const formattedTime = now.toLocaleTimeString();
const formattedDateTime = now.toLocaleString('en-IN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric',
    timeZoneName: 'short'
});

console.log(formattedDate); // Output: 11/22/2024 (example)
console.log(formattedTime); // Output: 5:10:11 PM (example)
console.log(formattedDateTime); // Output: 22 November 2024 at 5:08:57 pm IST (example)

Example: Custom Formatting

let now = new Date();
let options = { year: 'numeric', month: 'long', day: 'numeric' };
let formattedDate = new Intl.DateTimeFormat('en-US', options).format(now);
console.log(formattedDate); // Output: November 22, 2024 (example)

✦ Time Differences:

To calculate the difference between two given dates using the getTime() method, which returns the number of milliseconds since the Unix epoch.

const date1 = new Date(2024, 11, 1);
const date2 = new Date(2025, 0, 1);

const timeDifference = date2.getTime() - date1.getTime();
const daysDifference = timeDifference / (1000 * 60 * 60 * 24);

console.log(timeDifference); // Output: 2678400000
console.log(daysDifference); // Output: 31

✦ Time Zones:

To work with different time zones using the getTimezoneOffset() method.

const offset = now.getTimezoneOffset(); // Minutes offset from UTC

To create a Date object in a specific time zone, by using the toLocaleString() method with appropriate options.

✦ Setting a Time:

To set a specific components of a Date object.

// Current time
const date = new Date();

date.setFullYear(2025);
date.setMonth(0); // January
date.setDate(1);
date.setHours(12);
date.setMinutes(0);
date.setSeconds(0);

console.log(date); // Output: Wed Jan 01 2025 12:00:00 GMT+0530 (India Standard Time)

Example: Short way to specify a date

let newYear = new Date(2025, 0, 31); // Month is 0-indexed (January is 0)
console.log(newYear); // Output: Fri Jan 31 2025 00:00:00 GMT+0530 (India Standard Time)

⇪ To Top

❮ Previous TopicNext Topic ❯

⌂ Goto Home Page