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

在我的脚本标签中,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()?


蝴蝶不菲
浏览 279回答 3
3回答

江户川乱折腾

使用 PHP 获取今天的日期并更改格式$origDate = "2019-01-15";$newDate = date("m-d-Y", strtotime($origDate));echo $newDate;输出01-15-2019用js&nbsp; var date&nbsp; = new Date();&nbsp; var year=date.getFullYear();&nbsp; var month= date.getMonth();&nbsp; var date=date.getDay();&nbsp;console.log(` ${day}:${month}:${year}`);

ITMISS

当您已经拥有获得所需格式的所有内容时,请不要使用服务器端逻辑:<script>&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; let today = new Date();&nbsp; &nbsp; &nbsp; &nbsp; let strToday = today.getFullYear() + "-" + today.getMonth() + "-" + today.getDay();&nbsp; &nbsp; &nbsp; &nbsp; console.log(strToday);&nbsp; &nbsp; });</script>

RISEBY

只需格式化日期。不要忘记 getMonth() 是从零开始的。const date = new Date();const mm = date.getMonth() + 1;const dd = date.getDate();const format = [&nbsp; date.getFullYear(),&nbsp; (mm > 9 ? '' : '0') + mm,&nbsp; (dd > 9 ? '' : '0') + dd].join('-');console.log(format);
打开App,查看更多内容
随时随地看视频慕课网APP