为了账号安全,请及时绑定邮箱和手机立即绑定

我想将日期格式更改为“yyyy-mm-dd”

我想将日期格式更改为“yyyy-mm-dd”

PHP
蝴蝶不菲 2022-01-02 20:00:30
在我的脚本标签中,var today以Sat Sep 07 2019 00:00:00 GMT+0530 (India Standard Time)日期格式显示,我想将此日期格式更改为yyyy-mm-dd格式。我的代码:<script>    $(document).ready(function() {        var date  = new Date();        var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());        console.log(today);    });</script>我正在尝试这样:<script>    $(document).ready(function() {        var date  = new Date();        var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());        var newDate = "<?php echo date("Y-m-d", strtotime(today)); ?>";        console.log(newDate);    });</script>如何使用 更改日期格式strtotime()?
查看完整描述

3 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

使用 PHP 获取今天的日期并更改格式



$origDate = "2019-01-15";


$newDate = date("m-d-Y", strtotime($origDate));

echo $newDate;


输出


01-15-2019


用js


  var date  = new Date();

  var year=date.getFullYear();

  var month= date.getMonth();

  var date=date.getDay();


 console.log(` ${day}:${month}:${year}`);


查看完整回答
反对 回复 2022-01-02
?
ITMISS

TA贡献1871条经验 获得超8个赞

当您已经拥有获得所需格式的所有内容时,请不要使用服务器端逻辑:


<script>

    $(document).ready(function() {

        let today = new Date();

        let strToday = today.getFullYear() + "-" + today.getMonth() + "-" + today.getDay();

        console.log(strToday);

    });

</script>


查看完整回答
反对 回复 2022-01-02
?
RISEBY

TA贡献1856条经验 获得超5个赞

只需格式化日期。不要忘记 getMonth() 是从零开始的。


const date = new Date();


const mm = date.getMonth() + 1;

const dd = date.getDate();


const format = [

  date.getFullYear(),

  (mm > 9 ? '' : '0') + mm,

  (dd > 9 ? '' : '0') + dd

].join('-');


console.log(format);


查看完整回答
反对 回复 2022-01-02
  • 3 回答
  • 0 关注
  • 278 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号