日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php datediff 函数,dateAdd与DateDiff函数的js代码

發布時間:2025/3/8 php 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php datediff 函数,dateAdd与DateDiff函数的js代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、DateAdd函數:

復制代碼 代碼示例:

function DateAdd(interval,number,date){

switch(interval.toLowerCase()){

case "y": return new Date(date.setFullYear(date.getFullYear()+number));

case "m": return new Date(date.setMonth(date.getMonth()+number));

case "d": return new Date(date.setDate(date.getDate()+number));

case "w": return new Date(date.setDate(date.getDate()+7*number));

case "h": return new Date(date.setHours(date.getHours()+number));

case "n": return new Date(date.setMinutes(date.getMinutes()+number));

case "s": return new Date(date.setSeconds(date.getSeconds()+number));

case "l": return new Date(date.setMilliseconds(date.getMilliseconds()+number));

}

}

2、DateDiff函數:

復制代碼 代碼示例:

function DateDiff(interval,date1,date2){

var long = date2.getTime() - date1.getTime(); //相差毫秒

switch(interval.toLowerCase()){

case "y": return parseInt(date2.getFullYear() - date1.getFullYear());

case "m": return parseInt((date2.getFullYear() - date1.getFullYear())*12 + (date2.getMonth()-date1.getMonth()));

case "d": return parseInt(long/1000/60/60/24);

case "w": return parseInt(long/1000/60/60/24/7);

case "h": return parseInt(long/1000/60/60);

case "n": return parseInt(long/1000/60);

case "s": return parseInt(long/1000);

case "l": return parseInt(long);

}

}

3、兼容多瀏覽器的datediff函數

復制代碼 代碼示例:

function NewDate(str) {

str = str.split('-');

var date = new Date();

date.setUTCFullYear(str[0], str[1] - 1, str[2]);

date.setUTCHours(0, 0, 0, 0);

return date;

}

function TimeCom(dateValue) {

var newCom;

if (dateValue == "") {

newCom = new Date();

} else {

newCom = NewDate(dateValue);

}

this.year = newCom.getYear();

this.month = newCom.getMonth() + 1;

this.day = newCom.getDate();

this.hour = newCom.getHours();

this.minute = newCom.getMinutes();

this.second = newCom.getSeconds();

this.msecond = newCom.getMilliseconds();

this.week = newCom.getDay();

}

function DateDiff(interval, date1, date2) {

var TimeCom1 = new TimeCom(date1);

var TimeCom2 = new TimeCom(date2);

var result;

switch (String(interval).toLowerCase()) {

case "y":

case "year":

result = TimeCom1.year - TimeCom2.year;

break;

case "m":

case "month":

result = (TimeCom1.year - TimeCom2.year) * 12 + (TimeCom1.month - TimeCom2.month);

break;

case "d":

case "day":

result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day)) / (1000 * 60 * 60 * 24));

break;

case "h":

case "hour":

result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour)) / (1000 * 60 * 60));

break;

case "min":

case "minute":

result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute)) / (1000 * 60));

break;

case "s":

case "second":

result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second)) / 1000);

break;

case "ms":

case "msecond":

result = Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second, TimeCom1.msecond) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second, TimeCom1.msecond);

break;

case "w":

case "week":

result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day)) / (1000 * 60 * 60 * 24)) % 7;

break;

default:

result = "invalid";

}

return (result);

}

總結

以上是生活随笔為你收集整理的php datediff 函数,dateAdd与DateDiff函数的js代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。