Format Date Javascript

Asked
Active3 hr before
Viewed126 times

8 Answers

formatjavascript
90%

JavaScript provides Date object to work with date & time, including days, months, years, hours, minutes, seconds, and milliseconds, , Specify seven numeric values to create a date object with the specified year, month and optionally date, hours, minutes, seconds and milliseconds

Example_snippet/controller/utility/_format.js/ Date(); //Returns current dat. . .
Date(); //Returns current date and time string

//or

var currentDate = new Date(); //returns date object of current date and time
load more v
88%

Published Nov 01, 2019 ,How to validate an email address in JavaScript,How to detect if an Adblocker is being used with JavaScript,How to upload files to the server using JavaScript

Example_snippet/controller/utility/_format.js/ const date = new Date('July 22. . .
const date = new Date('July 22, 2018 07:22:13')
load more v
72%

The string format should be: YYYY-MM-DDTHH:mm:ss,sssZ, where:,The method Date

Example_snippet/controller/utility/_format.js/ let now = new Date(); alert( n. . .
let now = new Date();
alert(now); // shows current date/time
load more v
65%

JavaScript Date objects represent a single moment in time in a platform-independent format, Date objects contain a Number that represents milliseconds since 1 January 1970 UTC

Example_snippet/controller/utility/_format.js/ let today = new Date() let bir. . .
let today = new Date()
let birthday = new Date('December 17, 1995 03:24:00')
let birthday = new Date('1995-12-17T03:24:00')
let birthday = new Date(1995, 11, 17) // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0)
let birthday = new Date(628021800000) // passing epoch timestamp
load more v
75%

Meta Stack Overflow , Outdated Answers: accepted answer is now unpinned on Stack Overflow ,In JavaScript, how can I format a date object to print as 10-Aug-2010?, Stack Overflow help chat

Example_snippet/controller/utility/_format.js/ var options = { weekday: 'long. . .
var options = {
   weekday: 'long',
   year: 'numeric',
   month: 'long',
   day: 'numeric'
};
var today = new Date();

console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016
load more v
40%

In JavaScript, if you want to use a date string, you need to use a format that’s accepted worldwide, One of these formats is the ISO 8601 Extended format

Example_snippet/controller/utility/_format.js/ new Date('1988-03-21'). . .
new Date('1988-03-21')
Step 2 continued with // ISO 8601 Extended format `Y. . .
// ISO 8601 Extended format
`YYYY-MM-DDTHH:mm:ss.sssZ`
Step 3 continued with new Date('2019-06-11'). . .
new Date('2019-06-11')
Step 4 continued with new Date('2019-06-11T00:00'). . .
new Date('2019-06-11T00:00')
Step 5 continued with // 11th June 2019, 5:23:59am, . . .
// 11th June 2019, 5:23:59am, Local Time
new Date(2019, 5, 11, 5, 23, 59)
Step 6 continued with new Date(2017, 3, 22, 5, 23, 5. . .
new Date(2017, 3, 22, 5, 23, 50)

// This date can be easily read if you follow the left-right formula.
// Year: 2017,
// Month: April (because month is zero-indexed)
// Date: 22
// Hours: 05
// Minutes: 23
// Seconds: 50
Step 7 continued with // 21st March 1988, 12am, Loca. . .
// 21st March 1988, 12am, Local Time.
new Date(1988, 2, 21)

// 25th December 2019, 8am, Local Time.
new Date(2019, 11, 25, 8)

// 6th November 2023, 2:20am, Local Time
new Date(2023, 10, 6, 2, 20)

// 11th June 2019, 5:23:59am, Local Time
new Date(2019, 5, 11, 5, 23, 59)
Step 8 continued with // 11th June 2019, 12am, UTC. . . .
// 11th June 2019, 12am, UTC.
new Date(Date.UTC(2019, 5, 11))
Step 9 continued with // 11th June 2019, 8am (in my . . .
// 11th June 2019, 8am (in my Local Time, Singapore)
new Date(1560211200000)
Step 10 continued with new Date(). . .
new Date()
Step 11 continued with const date = new Date(2019, 0,. . .
const date = new Date(2019, 0, 23, 17, 23, 42)
Step 12 continued with const d = new Date(2019, 0, 23. . .
const d = new Date(2019, 0, 23)
const year = d.getFullYear() // 2019
const date = d.getDate() // 23
Step 13 continued with const months = { 0: 'January. . .
const months = {
   0: 'January',
   1: 'February',
   2: 'March',
   3: 'April',
   4: 'May',
   5: 'June',
   6: 'July',
   7: 'August',
   8: 'September',
   9: 'October',
   10: 'November',
   11: 'December'
}
Step 14 continued with const months = [ 'January', . . .
const months = [
   'January',
   'February',
   'March',
   'April',
   'May',
   'June',
   'July',
   'August',
   'September',
   'October',
   'November',
   'December'
]
Step 15 continued with const monthIndex = d.getMonth(. . .
const monthIndex = d.getMonth()
const monthName = months[monthIndex]
console.log(monthName) // January
Step 16 continued with const monthName = months[d.get. . .
const monthName = months[d.getMonth()]
console.log(monthName) // January
Step 17 continued with const days = [ 'Sun', 'Mon. . .
const days = [
   'Sun',
   'Mon',
   'Tue',
   'Wed',
   'Thu',
   'Fri',
   'Sat'
]
Step 18 continued with const dayIndex = d.getDay() co. . .
const dayIndex = d.getDay()
const dayName = days[dayIndex] // Thu
Step 19 continued with const dayName = days[d.getDay(. . .
const dayName = days[d.getDay()] // Thu
Step 20 continued with const formatted = `${dayName},. . .
const formatted = `${dayName}, ${date} ${monthName} ${year}`
console.log(formatted) // Thu, 23 January 2019
Step 21 continued with const earlier = new Date(2019,. . .
const earlier = new Date(2019, 0, 26)
const later = new Date(2019, 0, 27)

console.log(earlier < later) // true
Step 22 continued with const a = new Date(2019, 0, 26. . .
const a = new Date(2019, 0, 26)
const b = new Date(2019, 0, 26)

console.log(a == b) // false
console.log(a === b) // false
Step 23 continued with const isSameTime = (a, b) => {. . .
const isSameTime = (a, b) => {
   return a.getTime() === b.getTime()
}

const a = new Date(2019, 0, 26)
const b = new Date(2019, 0, 26)
console.log(isSameTime(a, b)) // true
Step 24 continued with const isSameDay = (a, b) => { . . .
const isSameDay = (a, b) => {
   return a.getFullYear() === b.getFullYear() &&
      a.getMonth() === b.getMonth() &&
      a.getDate() === b.getDate()
}

const a = new Date(2019, 0, 26, 10) // 26 Jan 2019, 10am
const b = new Date(2019, 0, 26, 12) // 26 Jan 2019, 12pm
console.log(isSameDay(a, b)) // true
Step 25 continued with const d = new Date(2019, 0, 10. . .
const d = new Date(2019, 0, 10)
d.setDate(15)

console.log(d) // 15 January 2019
Step 26 continued with const d = new Date(2019, 0, 1. . .
const d = new Date(2019, 0, 10)
d.setMonth(5)

console.log(d) // 10 June 2019
Step 27 continued with const d = new Date(2019, 0, 10. . .
const d = new Date(2019, 0, 10)
const newDate = new Date(d)
newDate.setMonth(5)

console.log(d) // 10 January 2019
console.log(newDate) // 10 June 2019
Step 28 continued with // Assumes today is 28 March 2. . .
// Assumes today is 28 March 2019
const today = new Date(2019, 2, 28)
Step 29 continued with const finalDate = new Date(tod. . .
const finalDate = new Date(today)
Step 30 continued with const currentDate = today.getD. . .
const currentDate = today.getDate()
Step 31 continued with finalDate.setDate(currentDate . . .
finalDate.setDate(currentDate + 3)
Step 32 continued with const today = new Date(2019, 2. . .
const today = new Date(2019, 2, 28)
const finalDate = new Date(today)
finalDate.setDate(today.getDate() + 3)

console.log(finalDate) // 31 March 2019
Step 33 continued with const today = new Date(2019, 2. . .
const today = new Date(2019, 2, 28)

// Getting required values
const year = today.getFullYear()
const month = today.getMonth()
const day = today.getDate()

// Creating a new Date (with the delta)
const finalDate = new Date(year, month, day + 3)

console.log(finalDate) // 31 March 2019
Step 34 continued with // 33rd March => 2nd April new. . .
// 33rd March => 2nd April
new Date(2019, 2, 33)
Step 35 continued with // 33rd March => 2nd April new. . .
// 33rd March => 2nd April
new Date(2019, 2, 30 + 3)
load more v
22%

The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:,ISO 8601 is the international standard for the representation of dates and times,,There are generally 3 types of JavaScript date input formats:,The ISO format follows a strict standard in JavaScript

Example_snippet/controller/utility/_format.js/ Date.parse(). . .
Date.parse()
load more v
60%

In this example we used the toLocaleString() method to apply the “English-US” time format, The output matches the US English common time format: D/MM/YYYY HH:MM:SS AM/PM

Example_snippet/controller/utility/_javascript.js/ let date = new Date(); console. . .
let date = new Date();
console.log(date);
// Output: Tue Jul 21 2020 10:01:14 GMT+0100 (UK Daylight Time)
load more v