今天三个月前如何计算JavaScript的日期?

编程入门 行业动态 更新时间:2024-10-23 03:15:58
本文介绍了今天三个月前如何计算JavaScript的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在当前日期前3个月形成日期。我通过以下代码获得当前月份$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ var currentMonth = currentDate.getMonth()+ 1;你可以为我提供计算和形成一个日期的逻辑(一个<$ c的对象)

考虑到当月份为1月(1)时,日期为$ c> Date 数据类型),日期前3个月将为OCtober(10)?

解决方案

var d = new Date(); d.setMonth(d.getMonth() - 3);

这适用于1月份。运行此代码段:

var d = new Date(2012-01-15); document.write(d +< br />); d.setMonth(d.getMonth() - 3); document.write(d);

p>

I Am trying to form a date which is 3 months before the current date. I get the current month by the below code

var currentDate = new Date(); var currentMonth = currentDate.getMonth()+1;

Can you guys provide me the logic to calculate and form a date (an object of the Date data type) considering that when the month is January (1), 3 months before date would be OCtober (10)?

解决方案

var d = new Date(); d.setMonth(d.getMonth() - 3);

This works for January. Run this snippet:

var d = new Date("2012-01-15"); document.write(d + "<br/>"); d.setMonth(d.getMonth() - 3); document.write(d);

更多推荐

今天三个月前如何计算JavaScript的日期?

本文发布于:2023-10-16 00:22:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:月前   日期   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!