js计算当前时间与相差天数的代码

发布时间:2019-08-25编辑:脚本学堂
分享一例js计算当前时间与相差天数的代码,js天数计算的小例子,有需要的朋友参考下。

例子,js时间计算求相差天数。
 

复制代码 代码示例:
function diffDays(s1,s2)//计算相差的天数
{
 s1 = s1.replace(/-/g, "/");
 s2 = s2.replace(/-/g, "/");
 s1 = new Date(s1);
 s2 = new Date(s2);
 
 var days= s2.getTime() - s1.getTime();
 var time = parseInt(days / (1000 * 60 * 60 * 24));
 
 return time;
}
function getNow(){//得到当前时间
 today = new Date();
 
 var todayStr = today.getYear() + "-";
 if(today.getMonth()<10){
  todayStr=todayStr+"0"+(parseInt(today.getMonth())+1);
 }
 else{
  todayStr=todayStr+(parseInt(today.getMonth())+1);
 }
 
 if(today.getDay()<10){
  todayStr=todayStr+"-0"+(parseInt(today.getDay())+1);
 }
 else{
  todayStr=todayStr+(parseInt(today.getDay())+1);
 }
  todayStr = todayStr +" "
 
 if(today.getHours()<10){
  todayStr=todayStr+today.getHours();
 }
 else{
  todayStr=todayStr+today.getHours();
 }
 
 if(today.getMinutes()<10){
  todayStr=todayStr+":0"+today.getMinutes();
 }
 else{
  todayStr=todayStr+":"+today.getMinutes();
 }
  return todayStr = todayStr + ":00";
}