首页 > web前端 > js教程 > 正文

怎么使用JavaScript操作日期和时间?

狼影
发布: 2025-09-16 22:51:01
原创
777人浏览过
<blockquote>JavaScript中操作日期时间需使用Date对象,通过new Date()创建,支持获取当前或指定时间。获取年月日可用getFullYear()、getMonth()(需+1)和getDate(),并结合padStart()格式化补零。格式化推荐Intl.DateTimeFormat或手动拼接字符串,避免Moment.js等重型库。比较日期应使用getTime()转换为毫秒数进行大小判断,避免直接比较引用。注意月份从0开始及时区影响。</blockquote> <p><img src="https://img.php.cn/upload/article/001/253/068/175803426479859.png" alt="怎么使用javascript操作日期和时间?"></p> <p>JavaScript操作日期和时间,简单来说,就是用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Date</pre>
登录后复制
</div>对象来创建、格式化、计算日期和时间。它不像Python的<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">datetime</pre>
登录后复制
</div>那么直观,但掌握了基本方法,也能应付大部分场景。</p> <p>解决方案:</p> <p>JavaScript的<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Date</pre>
登录后复制
</div>对象是核心。你可以用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">new Date()</pre>
登录后复制
</div>创建,不带参数是当前时间,带参数可以是毫秒数、日期字符串,甚至年月日时分秒。</p> <p>比如:</p> <p><span>立即学习</span>“<a href="https://pan.quark.cn/s/c1c2c2ed740f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Java免费学习笔记(深入)</a>”;</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>let now = new Date(); // 当前时间 let specificDate = new Date('2023-12-25'); // 指定日期 let anotherDate = new Date(2024, 0, 1); // 2024年1月1日 (月份从0开始)</pre>
登录后复制
</div><p>格式化日期,可以用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">toLocaleDateString()</pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">toLocaleTimeString()</pre>
登录后复制
</div>等,但灵活性有限。更常用的方法是自己拼字符串,或者用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Intl.DateTimeFormat</pre>
登录后复制
</div>,或者干脆引入Moment.<a style="color:#f60; text-decoration:underline;" title="js" href="https://www.php.cn/zt/15802.html" target="_blank">js</a>(虽然现在不推荐了,但老项目里常见)。</p> <p>计算日期,可以直接加减毫秒数。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Date.getTime()</pre>
登录后复制
</div>返回毫秒数,加减之后再<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">new Date()</pre>
登录后复制
</div>一下。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>let tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000); // 明天</pre>
登录后复制
</div><p>还有<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">setDate()</pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">setMonth()</pre>
登录后复制
</div>等方法,但用起来容易出错,推荐用毫秒数计算。</p> <p>JavaScript日期处理的坑在于时区和月份。时区默认是<a style="color:#f60; text-decoration:underline;" title="浏览器" href="https://www.php.cn/zt/16180.html" target="_blank">浏览器</a>所在时区,服务器端处理日期要特别注意。月份是从0开始的,用的时候要小心。</p> <h3>如何在JavaScript中获取当前年份、月份和日期?</h3> <p>获取年月日,可以用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">getFullYear()</pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">getMonth()</pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">getDate()</pre>
登录后复制
</div>。注意<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">getMonth()</pre>
登录后复制
</div>返回的是0-11,要加1才是实际月份。</p> <div class="aritcle_card"> <a class="aritcle_card_img" href="/ai/1176"> <img src="https://img.php.cn/upload/ai_manual/001/431/639/68b7b00a91a23666.png" alt="AI Room Planner"> </a> <div class="aritcle_card_info"> <a href="/ai/1176">AI Room Planner</a> <p>AI 室内设计工具,免费为您的房间提供上百种设计方案</p> <div class=""> <img src="/static/images/card_xiazai.png" alt="AI Room Planner"> <span>136</span> </div> </div> <a href="/ai/1176" class="aritcle_card_btn"> <span>查看详情</span> <img src="/static/images/cardxiayige-3.png" alt="AI Room Planner"> </a> </div> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>let now = new Date(); let year = now.getFullYear(); let month = now.getMonth() + 1; let day = now.getDate(); console.log(`${year}-${month}-${day}`);</pre>
登录后复制
</div><p>有时候,你可能需要<a style="color:#f60; text-decoration:underline;" title="格式化输出" href="https://www.php.cn/zt/37682.html" target="_blank">格式化输出</a>,比如<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">YYYY-MM-DD</pre>
登录后复制
</div>。可以自己拼字符串,也可以用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">padStart()</pre>
登录后复制
</div>补零。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>let monthFormatted = String(month).padStart(2, '0'); let dayFormatted = String(day).padStart(2, '0'); console.log(`${year}-${monthFormatted}-${dayFormatted}`);</pre>
登录后复制
</div><h3>JavaScript日期格式化有哪些常见的方法?</h3> <p>格式化日期,最简单的就是<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">toLocaleDateString()</pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">toLocaleTimeString()</pre>
登录后复制
</div>,但定制性差。</p> <p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Intl.DateTimeFormat</pre>
登录后复制
</div>是更现代的方法,可以指定语言和格式。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>let now = new Date(); let formatter = new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }); console.log(formatter.format(now)); // 输出:2023/10/27</pre>
登录后复制
</div><p>Moment.js(或者Day.js)是老牌的日期处理库,功能强大,但体积较大。如果只是简单格式化,没必要引入。</p> <p>自己拼字符串是最灵活的方法,但代码比较繁琐。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>function formatDate(date) { let year = date.getFullYear(); let month = String(date.getMonth() + 1).padStart(2, '0'); let day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }</pre>
登录后复制
</div><h3>如何在JavaScript中比较两个日期?</h3> <p>比较日期,最简单的方法是比较它们的毫秒数。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Date.getTime()</pre>
登录后复制
</div>返回毫秒数,可以直接用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">></pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><</pre>
登录后复制
</div>、<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">==</pre>
登录后复制
</div>比较。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>let date1 = new Date('2023-10-26'); let date2 = new Date('2023-10-27'); if (date1.getTime() < date2.getTime()) { console.log('date1在date2之前'); }</pre>
登录后复制
</div><p>如果要忽略时间部分,只比较日期,可以先把时间部分设为0,再比较。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>function stripTime(date) { let newDate = new Date(date); newDate.setHours(0, 0, 0, 0); return newDate; } let date1Stripped = stripTime(date1); let date2Stripped = stripTime(date2); if (date1Stripped.getTime() == date2Stripped.getTime()) { console.log('date1和date2是同一天'); }</pre>
登录后复制
</div><p>注意,直接用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">==</pre>
登录后复制
</div>比较<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Date</pre>
登录后复制
</div>对象,比较的是引用,不是值。所以要用<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">getTime()</pre>
登录后复制
</div>或者自定义比较函数。</p>

以上就是怎么使用JavaScript操作日期和时间?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号