How to change date format using jQuery / javascript?

Today I want to convert the date to another format using jQuery / Javascript:

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

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

Thanks!

+4
source share
4 answers

You need a date in this 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 out

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

Then you can format the date as you wish.

Example for formatting a date object:

 $.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; }; 

Jquery plugin to help you format date like 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 it is.

+2
source

Use a 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> 
+4
source

If you are targeting new browsers, you can parse it directly into JavaScript, as shown in the fujy or LeGrande file. But when you do this, understand that the UTC date you submitted will be converted to the local time zone of the browser.

If you need more flexibility and full browser compatibility, 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") 
+2
source

JavaScript smart enough to accept dates in a different format

 var d1 = new Date('2013-04-01T19:45:11.000Z'); 
+1
source

Source: https://habr.com/ru/post/1499973/


All Articles