4

Today I want to transfrom date into different format using jQuery/Javascript :

$date = '2013-04-01T19:45:11.000Z'
$cool = date('Y-m-d',strtotime($date));

How can I do that (PHP) in jQuery/Javascript ?

Thanks!

416E64726577
  • 2,214
  • 2
  • 23
  • 47
Deptroco
  • 129
  • 1
  • 3
  • 11
  • check this post http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript – fujy Sep 01 '13 at 00:14
  • check solution here: http://stackoverflow.com/questions/19546855/change-date-format-from-jquery?answertab=oldest#tab-top – DropAndTrap Apr 08 '14 at 07:11

4 Answers4

6

Use the javascript date object.

<script>
  var date = new Date('2013-04-01T19:45:11.000Z');
  var day = date.getDate();
  var month = date.getMonth();
  var year = date.getFullYear();

  document.write(year + '-' + month + '-' + day);
</script>
AndyD273
  • 7,177
  • 12
  • 54
  • 92
LeGrande
  • 230
  • 1
  • 6
  • 1
    It's worth noting that .getMonth returns a zero based version of the month (January=0, February=1, December=11, etc). You should probably add one to the month if you are planning on returning a correct date this way: `var month = date.getMonth() + 1;` – AndyD273 Nov 08 '13 at 16:28
2

You need a date in such format for javascript Sun, 01 Sep 13 08:06:57 +0000 So in PHP you need to do something like this

date_default_timezone_set('Australia/Perth');
echo date('D, d M y H:i:s')." +0000";

And you can experiment with jQuery to check it

$.get('dateformat.php', function(data) {
  date = new Date(data);
  console.log(date);  
});

Then you could format a date as you wish

Example for formatting date object on your own:

$.date = function(dateObj) {
    var d = new Date(dateObj);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();

    if (month < 10) {
        month = "0" + month;
    }
    return year + "." + month + "." + day;
};

A jquery plugin to help you format a date similar to php

https://github.com/phstc/jquery-dateFormat

You can use it to apply a format like this

$.format.date("2009-12-18 10:54:50.546", "Test: dd/MM/yyyy")

I think that's it.

Davit
  • 1,394
  • 5
  • 21
  • 47
  • Why would you say you need that format for JavaScript? That's just not true. JavaScript supports a lot of different date formats natively. [Here are some examples](http://dygraphs.com/date-formats.html) – Matt Johnson-Pint Sep 02 '13 at 03:19
  • Op indicated he wanted to use something like 'Y-m-d' that's why I referred to a plugin, otherwise I wrote an example how to do it by hand – Davit Sep 02 '13 at 03:59
  • I was referring to the first sentence of your answer. – Matt Johnson-Pint Sep 02 '13 at 04:32
  • @Dachi, I am trying to format date with jquery date format plugin but not succeeded. Can you please give me an example with jsfiddle link using https://github.com/phstc/jquery-dateFormat plugin. – Rajeev Dec 11 '13 at 15:24
  • @Dachi, Thank you very much for sparing your busy time. Is it possible to insert any static text in time format. I am looking for a date format like Nov 15th @ 05a.m. I want 'th' after day of the month. How can i achieve this? I am using date format $.format.date("2013-11-15 05:44:30.516", "MMM d @ hhp") – Rajeev Dec 12 '13 at 03:06
  • @rajeevprasanna sorry for late response. `MMM D @ hhp` is that way to go in that case. http://jsfiddle.net/dnatsvlishvili/JZLP3/1/ – Davit Dec 13 '13 at 07:43
2

If you're aiming for newer browsers, then you can parse it directly in JavaScript like fujy or LeGrande showed. But when you do, understand that the UTC date you passed will be converted to the local time zone of the browser.

If you want more flexibility, and full browser compatibility, then you should use a library like moment.js.

// Parse it to a moment
var m = moment("2013-04-01T19:45:11.000Z");

// Format it in local time in whatever format you want
var s = m.format("YYYY-MM-DD  HH:mm:ss")

// Or treat it as UTC and then format it
var s = m.utc().format("YYYY-MM-DD  HH:mm:ss")
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

JavaScript is smart enough to accept dates in different format

var d1 = new Date('2013-04-01T19:45:11.000Z');
fujy
  • 5,168
  • 5
  • 31
  • 50