php 如何使用 jQuery/Javascript 更改日期格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18553936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:47:15  来源:igfitidea点击:

How to change date format using jQuery/Javascript?

phpjavascriptjquerydatedate-formatting

提问by Deptroco

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

今天我想使用 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 ?

我怎样才能在 jQuery/Javascript 中做到这一点(PHP)?

Thanks!

谢谢!

采纳答案by Davit

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

你需要一个这样格式的 javascript 日期Sun, 01 Sep 13 08:06:57 +0000所以在 PHP 中你需要做这样的事情

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

您可以尝试使用 jQuery 来检查它

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

一个 jquery 插件,可帮助您格式化类似于 php 的日期

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

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.

我想就是这样。

回答by LeGrande

Use the javascript date object.

使用javascript 日期对象

<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>

回答by Matt Johnson-Pint

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.

如果你的目标是更新的浏览器,那么你可以像 fujy 或 LeGrande 展示的那样直接在 JavaScript 中解析它。但是当您这样做时,请了解您传递的 UTC 日期将转换为浏览器的本地时区。

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

如果您想要更大的灵活性和完整的浏览器兼容性,那么您应该使用像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")

回答by fujy

JavaScript is smart enough to accept dates in different format

JavaScript 足够聪明,可以接受不同格式的日期

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