0

How to convert this type of a time

      Thu Jul 17 2014 09:52:30 GMT+0300 (Turkey Daylight Time)

to

      17 Jul 2014 09:52

and this

      17 Jul 2014
MIRMIX
  • 1,052
  • 2
  • 14
  • 40
  • http://www.datejs.com/ – techfoobar Jul 17 '14 at 06:57
  • you can read the answer under this question: http://stackoverflow.com/questions/3552461/how-to-format-javascript-date – Andy1210 Jul 17 '14 at 07:01
  • [moment.js](http://momentjs.com) is a widely used solution. It's easier and more stable than implementing by yourself, if you don't mind the extra 10kB js. – rhgb Jul 17 '14 at 07:28

2 Answers2

1

Try this code:

var formatDate = function (txt) {
    var dt = new Date(txt);
    var fmt = dt.getDate();
    var sp = " ";
    fmt += sp;
    fmt += ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][dt.getMonth()];
    fmt += sp;
    fmt += dt.getFullYear();
    return isNaN(dt) ? txt : fmt;
};

var formatDateTime = function (txt) {
    var dt = new Date(txt);
    var fmt = formatDate(txt);
    if (isNaN(dt)) {
        fmt = txt;
    } else {
        fmt += " ";
        fmt += dt.getHours();
        fmt += ":";
        fmt += dt.getMinutes();
    }
    return fmt;
};

console.log(formatDateTime("Thu Jul 17 2014 09:52:30 GMT+0300 (Turkey Daylight Time)"));

console.log(formatDate("Thu Jul 17 2014 09:52:30 GMT+0300 (Turkey Daylight Time)"));
0

Although JavaScript provides a bunch of methods for getting and setting parts of a date object, it lacks a simple way to format dates and times according to a user-specified mask.

Whateverdate.format("dd mmm yyyy hh:MM")
Whateverdate.format("dd mmm yyyy")

You can read more here

Mohit S
  • 13,723
  • 6
  • 34
  • 69