0

How to I modify the below function to output date in the desired format, please?

Sending date to be formatted:

Original format: 2015-10-27 21:41:22

var d = new Date(globalStore.data[i].DateReg);
var e = formatDate(d);

Date function:

function formatDate(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;//to show time
  return date.getDate() + "/" + date.getMonth() + "/" + date.getFullYear() + " ";
}

Desired format:

14 Nov 2015
Yeldar Kurmangaliyev
  • 30,498
  • 11
  • 53
  • 87
112233
  • 2,206
  • 2
  • 23
  • 71
  • 1
    There's no jQuery at all in your question. This keeps coming up all the time. Do you know what JavaScript is, and what jQuery is? – Amit Nov 27 '15 at 08:26
  • What is the usage of time formatting in your function? Why do you parse time, if you need `dd MMM yyyy`. – Yeldar Kurmangaliyev Nov 27 '15 at 08:28
  • `date.toLocaleDateString('en-US', {day: 'numeric', month: 'short', year: 'numeric'})` would have been nice but does not match your desired format. Using the 'it-IT' locale it gets really close to the desired output, only the case of the month changing. It's really wacky to use another country's locale anyway... You should try it with yours, you might have a nice surprise – Aaron Nov 27 '15 at 23:12
  • Possible duplicate of [How to format a JavaScript date](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – John Slegers Feb 19 '16 at 21:46

5 Answers5

0

Date formatting is very tedious, why not find an easy way.. Use moment.js pretty simple,

http://momentjs.com/

var date = moment().format('DD MMM YYYY')
Zohaib Ijaz
  • 17,180
  • 5
  • 28
  • 50
0

If you want 14 Nov 2015

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ];

var date = new Date();
var yourDateformat =  date.getDate() + " " + monthNames[date.getMonth()] + " " + date.getFullYear();
John
  • 3,256
  • 7
  • 31
  • 78
0

I suggest to use library moment.js

var d = moment(globalStore.data[i].DateReg, 'YYYY-MM-DD HH:mm:ss'); // Convert raw date type string to type Datetime
moment(d).format('DD MMM YYYY'); // 27 Nov 2015

OR

function formateDate(strDate) {
    return moment(globalStore.data[i].DateReg, 'YYYY-MM-DD HH:mm:ss').format('DD MMM YYYY');
}
0

Working fiddle.

Also i suggest momment.js lib, but if you can't use additional libraries you can achieve that using an array of short month names :

var shodtMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",  "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec"];

var d = new Date("2015-10-27 21:41:22");

var day   = d.getDate();
var month = shodtMonthNames[d.getMonth()];
var year  = d.getFullYear();

console.log( day + " " + month + " " + year );

Hope this helps.

Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88
0

It's a bit lame solution, but I don't think defining a new array to keep month names is necessary. You can use the ones you already have.

var date = new Date().toDateString().split(' ');
// 'Sat Nov 28 2015'
var result = date[2] +' '+ date[1] +' '+ date[3];
// '28 Nov 2015'
Gökay Gürcan
  • 1,062
  • 1
  • 10
  • 25