Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript date custom string format

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0, 200));

// request a weekday along with a long date
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(new Intl.DateTimeFormat('de-DE', options).format(date));
// → "Donnerstag, 20. Dezember 2012"

// an application may want to use UTC and make that visible
options.timeZone = 'UTC';
options.timeZoneName = 'short';
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
// → "Thursday, December 20, 2012, GMT"

// sometimes you want to be more precise
options = {
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  timeZone: 'Australia/Sydney',
  timeZoneName: 'short'
};
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
// → "2:00:00 pm AEDT"

// sometimes you want to be very precise
options.fractionalSecondDigits = 3; //number digits for fraction-of-seconds
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
// → "2:00:00.200 pm AEDT"

// sometimes even the US needs 24-hour time
options = {
  year: 'numeric', month: 'numeric', day: 'numeric',
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  hour12: false,
  timeZone: 'America/Los_Angeles'
};
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
// → "12/19/2012, 19:00:00"

// to specify options but use the browser's default locale, use 'default'
console.log(new Intl.DateTimeFormat('default', options).format(date));
// → "12/19/2012, 19:00:00"

// sometimes it's helpful to include the period of the day
options = {hour: "numeric", dayPeriod: "short"};
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
// → 10 at night
Comment

PREVIOUS NEXT
Code Example
Javascript :: left join in sequelize 
Javascript :: restrict the user from going to signup or login page if the user is already logged in firebase auth 
Javascript :: date time js 
Javascript :: js remove if 
Javascript :: js window.alert 
Javascript :: javascript pluck from array of objects 
Javascript :: jest enzyme test receive submit 
Javascript :: convert number to boolean javascript 
Javascript :: href="javascript:void(null);" 
Javascript :: reset select form jquery 
Javascript :: angular refresh token 
Javascript :: react native get timezone 
Javascript :: styling element using jquery 
Javascript :: vuejs props declare prop with multiple types 
Javascript :: convert number to word js crore/lakh format 
Javascript :: jquery select input with class 
Javascript :: filter array inside array 
Javascript :: javascript folder dynamic import 
Javascript :: datatables filter with math functions 
Javascript :: pass id to reactjs routes 
Javascript :: simple ajax request 
Javascript :: discord.js how to send a message to all guilds 
Javascript :: scroll top js 
Javascript :: regular expressions password contains number 
Javascript :: react best libraries for Modern UI 
Javascript :: react native cross out letter 
Javascript :: link stylesheet in javascript 
Javascript :: regex for username 
Javascript :: fetch await reactjs 
Javascript :: every method javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =