gpt4 book ai didi

javascript格式化日期

转载 作者:行者123 更新时间:2023-11-29 16:27:19 24 4
gpt4 key购买 nike

假设日期当前日期是 2011 年 1 月 10 日。当我使用 js 代码获取日期时

var now = new Date();
var currentDate = now.getDate() + '-' + (now.getMonth() + 1) + '-' + now.getFullYear();

它返回“10-1-2011”
但我想要“10-01-2011”(2 位格式)

最佳答案

var now = new Date();
alert((now .getMonth() < 9 ? '0' : '') + (now .getMonth() + 1))

关于javascript格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com