2

I have following datetime value in a json :

Fri Jan 22 2016 14:34:38 GMT-0500

I would like to display something like "January 22, 2016"

How could I achieve this in javascript. I have JQuery, Extjs libraries available.

wpercy
  • 8,491
  • 4
  • 29
  • 39
Sri
  • 1,119
  • 1
  • 18
  • 41
  • 3
    Possible duplicate of [How to format a JavaScript date](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) –  Jan 26 '16 at 15:56
  • check the `moment.js` library, you can then use `moment("Fri Jan 22 2016 14:34:38 GMT-0500").format("LL")` to get your formatted date – Sabaz Jan 26 '16 at 15:57

3 Answers3

1

Try creating object having properties of abbreviated months, values of full month, using for..in loop , String.prototype.slice(), String.prototype.replace()

var months = {
 "Jan":"January",
  "Feb":"February",
  "Mar":"March",
  "Apr":"April",
  "May":"May",
  "Jun":"June",
  "Jul":"July",
  "Aug":"August",
  "Sep":"September",
  "Oct":"October",
  "Nov":"November",
  "Dec":"December"
};

var date = "Fri Jan 22 2016 14:34:38 GMT-0500";
// extract "Jan 22 2016" from `date`
var d = date.slice(4, -18);

for (var prop in months) {
  if (new RegExp(prop).test(d)) {
    // replace abbreviated month with full month name
    d = d.replace(prop, months[prop]);
    // replace day with day followed by comma `,` character
    d = d.replace(/(\d{2})(?=\s)/, "$1,")
  }
}

document.body.textContent = d
guest271314
  • 1
  • 10
  • 82
  • 156
0

This question is addressed to the same question of yours. You can use the functions shown here to construct the date string as you want.

// This could be any Date String
var str = "Fri Feb 08 2013 09:47:57 GMT +0530 (IST)";
var date = new Date(str);

This will then give you access to all the Date functions (MDN)

For example:

var day = date.getDate(); //Date of the month: 2 in our example
var month = date.getMonth(); //Month of the Year: 0-based index, so 1 in our example
var year = date.getFullYear() //Year: 2013

Extract date and time from string using Javascript

Community
  • 1
  • 1
P. Jairaj
  • 1,013
  • 1
  • 6
  • 8
0

Found this crazy method here, but worked!

Converting milliseconds to a date (jQuery/JS)

Here is the fiddle that i'v done

https://jsfiddle.net/Ripper1992/hj6L2Lvz/

var now = new Date("Fri Jan 22 2016 14:34:38 GMT-0500");
alert(now.customFormat( "#MMMM# #DD#, #YYYY#" ) );

customFormat is the function called to get each part of the Data, parse and replace based on the #MMMM# or #DD# or #SS# defined by the user.

And here is the complete function with the documentation http://phrogz.net/JS/FormatDateTime_JS.txt

Community
  • 1
  • 1
Leonardo Lima
  • 100
  • 10