How to display a Date object in a specific format using JavaScript?

I have a Date object, and I would like to display it in the following format:

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

How is this possible?

Thanks,

+8
javascript date html datetime
source share
6 answers

There is a great JavaScript library that does a great job of this, and is reduced to only 5.5KB.

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 dates as a String with a format or as 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"); 

There is also excellent support for languages ​​other than English, such as Russian, Japanese, Arabic, Spanish and others.

Check documents .

+11
source share

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; // eg "13 Nov 2016 11:00pm"; alert(myDate.getDate()+" "+month[myDate.getMonth()]+" "+myDate.getFullYear()+" "+strTime); </script> 
+7
source share

There are many date formatting packages available for javascript, I have achieved great success with Stephen Levitan dateformat .

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

Edit: It also adds a format method to Date.prototype if you like this style:

 getDate().format("dd mmm yyyy hh:MMtt"); 
+4
source share

Take a look at the Date() object: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

There are many useful methods ....

+1
source share

I think this may help you:

http://www.mattkruse.com/javascript/date/

There is a getDateFromFormat () function that you can modify a bit to solve your problem.

0
source share

You can display a different date format using the Date object in JavaScript. Another date format may be

  1. Only a year (YYYY)
  2. Year and month (YYYY-MM)
  3. Full date (YYYY-MM-DD)
  4. Full day with the name of the day (YYYY-MM-DD DayName)
  5. Date in a different time format

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

 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 useful to learn more about displaying other date formats: https://www.siteforinfotech.com/2014/03/how-to-display-date-format-in-javascript.html

0
source share

All Articles