js实现日期显示的一些操作(示例说明)
1、JS获取当前日期(yyyy-mm-dd)下面的代码是当前获得的日期:
var创建=新日期();
var = mydate.getfullyear(年); / /获得全年(4, 1970)
VaR月= mydate.getmonth(+ 1); / /获取当前月(1-12)
var = mydate.getdate(天); / /获取当前天(1-31)
获取完整日期
VaR天=年+月+日+;
2。点击日期加减(yyyy-mm-dd)
单击这两个按钮可以分别添加和减少日期。如果天数最多,月份会自动增加或减少。
var = 0;
DayChange(0)
$(#时间添加)。Click(function(){)
氮+;
DayChange(N);
})
$(#时间少)。Click(function(){)
n;
DayChange(N);
})
功能daychange(N){
var现在是新的(日期);
VaR TOMO =新(日期(现在/ 1000 + 86400 * N)×1000); / /明天
VaR月= tomo.getmonth()+ 1;
无功strdate = tomo.getdate();
无功seperator1 = ;
如果(月> = 1个月< = 9){
月= 0+月份;
}
如果(strdate > = 0 strdate <= 9){
strdate =0+ strdate;
}
无功currentdate = tomo.getfullyear()+ seperator1 +月+ seperator1 + strdate;
$(。中心天)Html(currentdate);
}
三.了解本周星期一和星期日
不管当前一周,您可以得到当前一周的开始时间。
var现在是新的(日期);
星期(现在);
函数周(现在){
VaR now.gettime数量=();
VaR天= now.getday();
无功onedaylong = 24 * 60 * 60 * 1000;
本周的星期一/访问
无功mondaytime =数量-(-1)* onedaylong;
本周的周末/收购
VaR星期=数量+(7天)* onedaylong;
转换日期
VaR星期一=新的日期(mondaytime);
星期日=新的日期(星期VaR);
VaR月= monday.getmonth()+ 1;
无功strdate = monday.getdate();
无功month1 = sunday.getmonth()+ 1;
无功strdate1 = sunday.getdate();
如果(月> = 1个月< = 9){
月= 0+月份;
}
如果(month1 > = 1 month1 <= 9){
month1 =0+ month1;
}
如果(strdate > = 0 strdate <= 9){
strdate =0+ strdate;
}
如果(strdate1 > = 0 strdate1 <= 9){
strdate1 =0+ strdate1;
}
currentdate = monday.getfullyear()+ seperator1 +月+ seperator1 + strdate +去+ sunday.getfullyear()+ + + + strdate1 seperator1 seperator1 month1;
$(。中心天)Html(currentdate);
32 }
4,点击每周的变化范围
单击更改按钮将更改显示一周的范围。如果在下个月或明年有一些变化,它会自动显示没有错误。
var现在是新的(日期);
var = 0;
星期(现在);
$(#周添加)。Click(function(){)
氮+;
VaR日期=新的日期(now.gettime()+ n * 7 * 24 * 3600 * 1000);
星期(日期);
})
$(#周添加)。Click(function(){)
n;
VaR日期=新的日期(now.gettime()+ n * 7 * 24 * 3600 * 1000);
星期(日期);
})
函数周(现在){
VaR now.gettime数量=();
VaR天= now.getday();
无功onedaylong = 24 * 60 * 60 * 1000;
本周的星期一/访问
无功mondaytime =数量-(-1)* onedaylong;
本周的周末/收购
VaR星期=数量+(7天)* onedaylong;
22 转换日期
VaR星期一=新的日期(mondaytime);
星期日=新的日期(星期VaR);
VaR月= monday.getmonth()+ 1;
无功strdate = monday.getdate();
无功month1 = sunday.getmonth()+ 1;
无功strdate1 = sunday.getdate();
如果(月> = 1个月< = 9){
月= 0+月份;
}
如果(month1 > = 1 month1 <= 9){
month1 =0+ month1;
}
如果(strdate > = 0 strdate <= 9){
strdate =0+ strdate;
}
如果(strdate1 > = 0 strdate1 <= 9){
strdate1 =0+ strdate1;
}
currentdate = monday.getfullyear()+ seperator1 +月+ seperator1 + strdate +去+ sunday.getfullyear()+ + + + strdate1 seperator1 seperator1 month1;
$(。中心天)Html(currentdate);
}
5。得到当前月的第一天和最后一天
为了得到本月的第一天和最后一天,最后一天的日期是不固定的,可以得到到期日。
(0)monthfen
功能monthfen(N){
var现在是新的(日期);
第一次约会时=新的VaR(日期(现在/ 1000 + 86400 * N *现在。getdate)(* 1000)); / /明天
/本月的第一天
firstdate.setdate(1); / /第一天
var date =新的日期(第一次约会时);
VaR月= date.getmonth()+ 1;
无功strdate =0+ date.getdate();
/这个月的最后一天
VaR EndDate =新的日期(第一次约会时);
enddate.setmonth(firstdate.getmonth()+ 1);
(0)enddate.setdate;
VaR date1 =新的日期(结束日期);
无功month1 = date1.getmonth()+ 1;
无功strdate1 = date1.getdate();
如果(月> = 1个月< = 9){
月= 0+月份;
}
如果(month1 > = 1 month1 <= 9){
month1 =0+ month1;
}
currentdate = date.getfullyear()+ seperator1 +月+ seperator1 + strdate +去+ date1.getfullyear()+ + + + strdate1 seperator1 seperator1 month1;
$(。中心天)Html(currentdate);
}
6,点击实现当前月份的更改。
单击按钮将更改当前月份,最后一天的日期将自动更改。
(0)monthfen
var = 0;
$(#月添加)。Click(function(){)
氮+;
monthfen(N);
})
$(#月)。Click(function(){)
n;
monthfen(N);
})
功能monthfen(N){
var现在是新的(日期);
第一次约会时=新的VaR(日期(现在/ 1000 + 86400 * N *现在。getdate)(* 1000)); / /明天
/本月的第一天
firstdate.setdate(1); / /第一天
var date =新的日期(第一次约会时);
VaR月= date.getmonth()+ 1;
无功strdate =0+ date.getdate();
/这个月的最后一天
VaR EndDate =新的日期(第一次约会时);
enddate.setmonth(firstdate.getmonth()+ 1);
(0)enddate.setdate;
VaR date1 =新的日期(结束日期);
无功month1 = date1.getmonth()+ 1;
无功strdate1 = date1.getdate();
如果(月> = 1个月< = 9){
月= 0+月份;
}
如果(month1 > = 1 month1 <= 9){
month1 =0+ month1;
}
currentdate = date.getfullyear()+ seperator1 +月+ seperator1 + strdate +去+ date1.getfullyear()+ + + + strdate1 seperator1 seperator1 month1;
$(。中心天)Html(currentdate);
}
当然,有很多关于日期格式的更改和算法,如果有什么不能理解的东西,你可以一起讨论。
上面的js实现日期显示一些操作(实例讲解)是萧边分享给你的所有内容,希望能给大家一个参考,我希望你能支持它。