33

I have a Date object and I'd like to display it in the below format:

var myDate = getDate();
// this format: "13 Jan 2012 11:00am";

How would that be possible?

Thanks,

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
The Light
  • 26,341
  • 62
  • 176
  • 258
  • 1
    Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Heretic Monkey Apr 30 '20 at 18:35

6 Answers6

30

Use the build-in toLocaleString()

const date = new Date();

const formattedDate = date.toLocaleString("en-GB", {
  day: "numeric",
  month: "short",
  year: "numeric",
  hour: "numeric",
  minute: "2-digit"
});

console.log(formattedDate); // '18 Jan 2020, 18:20'
zendka
  • 1,074
  • 12
  • 11
14

EDIT: For modern apps (without IE support) please see the answer by @zendka https://stackoverflow.com/a/59802446/483616


There is a great JavaScript library that handles this very well, and only 5.5kb minified.

http://momentjs.com/

It looks something like this:

moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2013, 9:54:04 am
moment().subtract('days', 6).calendar(); // "last Tuesday at 9:53 AM"

You can also pass in dates as a String with a format, or a Date object.

var date = new Date();
moment(date); // same as calling moment() with no args

// Passing in a string date
moment("12-25-1995", "MM-DD-YYYY");

Also has great support for languages other than English, like Russian, Japanese, Arabic, Spanish and others.

Check out the docs.

knownasilya
  • 5,998
  • 4
  • 36
  • 59
  • 2
    momentjs has been a great library. However, it is now a "[legacy project in maintenance mode](https://momentjs.com/docs/#/-project-status/)". Consider using other libraries like Day.js or the builtin [Date methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). – zendka Oct 06 '21 at 13:57
13

If you do not want to use any libraries:

<script type="text/javascript">

  var myDate = new Date();

  var month=new Array();
  month[0]="Jan";
  month[1]="Feb";
  month[2]="Mar";
  month[3]="Apr";
  month[4]="May";
  month[5]="Jun";
  month[6]="Jul";
  month[7]="Aug";
  month[8]="Sep";
  month[9]="Oct";
  month[10]="Nov";
  month[11]="Dec";
  var hours = myDate.getHours();
  var minutes = myDate.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12;
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ampm;
  // e.g. "13 Nov 2016 11:00pm";
  alert(myDate.getDate()+" "+month[myDate.getMonth()]+" "+myDate.getFullYear()+" "+strTime);
</script>
Felix Livni
  • 1,164
  • 13
  • 24
George
  • 6,006
  • 6
  • 48
  • 68
  • 3
    myDate.getDay() gives you the day of the week. Use getDate() instead. Source: http://www.w3schools.com/jsref/jsref_obj_date.asp – Pablo Reyes Feb 11 '16 at 20:23
  • 1
    Good answer. These days, can rely on shorthand for array literal: `var month = ["Jan", "Feb", .., "Dec"];`. – ToolmakerSteve Nov 03 '19 at 11:48
7

There are many date formatting packages available for javascript, I've had great success with Steven Levithan's dateformat.

dateFormat(getDate(), "dd mmm yyyy hh:MMtt");

Edit: It also adds a format method to Date.prototype, if you enjoy that style:

getDate().format("dd mmm yyyy hh:MMtt");
Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
2

Another JavaScript built-in library that you could now use to format a date, is the Intl library.

// use formatToParts() to convert a date and put it into an array for the different parts of the date
var customDateArray = new Intl.DateTimeFormat('en-US', { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' }). formatToParts(new Date()),
  dateParts = {}

// This converts the array to an object to make it easier to pick out what you want using template literals.
customDateArray.map(({type, value}) => {
  dateParts[type] = value
})

console.log(`${dateParts.day} ${dateParts.month} ${dateParts.year} ${dateParts.hour}:${dateParts.minute}${dateParts.dayPeriod.toLowerCase()}`)

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

0

You can display different date format using Date object in JavaScript.The different date format may be

  1. Year only (YYYY)
  2. Year and month (YYYY-MM)
  3. Complete date (YYYY-MM-DD)
  4. Complete day with day name (YYYY-MM-DD DayName)
  5. Date with different time format

Use the following JavaScript code to display the date on the year only format.

function displayDate(){
var now = new Date();
var year=now.getFullYear();
date.innerHTML=year
}
window.onload=displayDate;
<div id="date"></div>

This post will be more helpful to know more about displaying other more date formats: https://www.siteforinfotech.com/2014/03/how-to-display-date-format-in-javascript.html

shuseel
  • 597
  • 6
  • 10