javascript tutorial - [Solved-5 Solutions] Format a JavaScript date - javascript - java script - javascript array



Problem:

How to format a JavaScript date?

Solution 1:

function formatDate(date) {
  var monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];

  var day = date.getDate();
  var monthIndex = date.getMonth();
  var year = date.getFullYear();

  return day + ' ' + monthNames[monthIndex] + ' ' + year;
}

console.log(formatDate(new Date()));  // show current date-time in console
click below button to copy the code. By JavaScript tutorial team

Solution 2:

Use the date.format library :

var dateFormat = require('dateformat');
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
click below button to copy the code. By JavaScript tutorial team

returns:

Saturday, June 9th, 2007, 5:46:21 PM 
click below button to copy the code. By JavaScript tutorial team

Solution 3:

var today = new Date().toISOString().slice(0, 10);
click below button to copy the code. By JavaScript tutorial team

sing this function instead:

function toJSONLocal (date) {
    var local = new Date(date);
    local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
    return local.toJSON().slice(0, 10);
}
click below button to copy the code. By JavaScript tutorial team

Solution 4:

  • quickly format the date using plain JavaScript, use getDatecode>, getMonth + 1, getFullYear, getHours and getMinutes:
var d = new Date();

var datestring = d.getDate()  + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " +
d.getHours() + ":" + d.getMinutes();

// 16-5-2015 9:50
click below button to copy the code. By JavaScript tutorial team

Solution 5:

Use toLocaleDateString();

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the date. The locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.

The values you can passed in options for different keys:

day:

  • The representation of the day.
  • Possible values are "numeric", "2-digit".

The representation of the day. Possible values are "numeric", "2-digit".

weekday:

  • The representation of the weekday.
  • Possible values are "narrow", "short", "long".

year:

  • The representation of the year.
  • Possible values are "numeric", "2-digit".

month:

  • The representation of the month.
  • Possible values are "numeric", "2-digit", "narrow", "short", "long".

hour:

  • The representation of the hour.
  • Possible values are "numeric", "2-digit".

minute:

  • The representation of the minute.
  • Possible values are "numeric", "2-digit".

second:

  • The representation of the second.
  • Possible values are "numeric", 2-digit".

All these keys are optional.You can change the number of options values based on your requirements.

For different languages:

  • "en-US": For English
  • "hi-IN": For Hindi
  • "ja-JP": For Japanese

You can use more language options.

For example

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today  = new Date();

console.log(today.toLocaleDateString("en-US"));
console.log(today.toLocaleDateString("en-US",options));
console.log(today.toLocaleDateString("hi-IN", options));

// Outputs will be -
9/17/2016
Saturday, September 17, 2016
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Format a JavaScript date