国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現(xiàn)的日期操作類DateTime函數(shù)代碼

2019-11-21 00:40:09
字體:
供稿:網(wǎng)友

方法注解:

將指定的天數(shù)加到此實例的值上。

將指定的小時數(shù)加到此實例的值上。

將指定的分鐘數(shù)加到此實例的值上。

將指定的毫秒數(shù)加到此實例的值上。

將指定的月份數(shù)加到此實例的值上。

將指定的秒數(shù)加到此實例的值上。

將指定的年份數(shù)加到此實例的值上。

將此實例的值與指定的 Date 值相比較,并指示此實例是早于、等于還是晚于指定的 Date 值。

返回一個數(shù)值相同的新DateTime對象

返回一個值,該值指示此實例是否與指定的 DateTime 實例相等。

獲取此實例的日期部分。

獲取此實例所表示的日期為該月中的第幾天。

獲取此實例所表示的日期是星期幾。

獲取此實例所表示日期的小時部分。

獲取此實例所表示日期的分鐘部分。

獲取此實例所表示日期的毫秒部分。

獲取此實例所表示日期的月份部分。

獲取此實例的下個月一日的DateTime對象

獲取此實例的下一個周日的DateTime對象

獲取此實例的下一個周日的DateTime對象

獲取此實例所表示日期的秒部分。

返回此實例的Date值

獲取此實例所表示日期的年份部分。

指示此實例是否是DateTime對象

將當前 DateTime 對象的值轉(zhuǎn)換為其等效的短日期字符串表示形式。

將當前 DateTime 對象的值轉(zhuǎn)換為其等效的短時間字符串表示形式。

將當前 DateTime 對象的值轉(zhuǎn)換為其等效的字符串表示形式。

驗證Add系列的方法參數(shù)是否合法

繼承自Date的方法

比較 DateTime 的兩個實例,并返回它們相對值的指示。

返回指定年和月中的天數(shù)。

返回一個值,該值指示 DateTime 的兩個實例是否相等。

返回指定的年份是否為閏年的指示。

獲取一個 DateTime 對象,該對象設(shè)置為此計算機上的當前日期和時間,表示為本地時間。

將日期和時間的指定字符串表示形式轉(zhuǎn)換為其等效的 DateTime。

獲取當前日期,其時間組成部分設(shè)置為 00:00:00。

復(fù)制代碼 代碼如下:

//表示時間上的一刻,通常以日期和當天的時間表示。
function DateTime(year, month, day, hour, min, sec, millisec){
    var d = new Date();

    if (year || year == 0){
        d.setFullYear(year);
    }
    if (month || month == 0){
        d.setMonth(month - 1);
    }
    if (day || day == 0){
        d.setDate(day);
    }
    if (hour || hour == 0){
        d.setHours(hour);
    }
    if (min || min == 0){
        d.setMinutes(min);
    }
    if (sec || sec == 0){
        d.setSeconds(sec);
    }
    if (millisec || millisec == 0){
        d.setMilliseconds(millisec);
    }
    //將指定的天數(shù)加到此實例的值上。
    this.AddDays = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setDate(result.GetDay() + value);
        return result;
    }
    //將指定的小時數(shù)加到此實例的值上。
    this.AddHours = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setHours(result.GetHour() + value);
        return result;
    }
    //將指定的分鐘數(shù)加到此實例的值上。
    this.AddMinutes = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setMinutes(result.GetMinute() + value);
        return result;
    }
    //將指定的毫秒數(shù)加到此實例的值上。
    this.AddMilliseconds = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setMilliseconds(result.GetMillisecond() + value);
        return result;
    }
    //將指定的月份數(shù)加到此實例的值上。
    this.AddMonths = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setMonth(result.GetValue().getMonth() + value);
        return result;
    }
    //將指定的秒數(shù)加到此實例的值上。
    this.AddSeconds = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setSeconds(result.GetSecond() + value);
        return result;
    }
    //將指定的年份數(shù)加到此實例的值上。
    this.AddYears = function(value){
        if(!ValidateAddMethodParam(value)){
            return null;
        }
        var result = this.Clone();
        result.GetValue().setFullYear(result.GetYear() + value);
        return result;
    }    
    //將此實例的值與指定的 Date 值相比較,并指示此實例是早于、等于還是晚于指定的 Date 值。
    this.CompareTo = function(other){
        var internalTicks = other.getTime();
        var num2 = d.getTime();
     if (num2 > internalTicks)
     {
     return 1;
     }
     if (num2 < internalTicks)
     {
     return -1;
     }
     return 0;
    }
    //返回一個數(shù)值相同的新DateTime對象
    this.Clone = function(){
        return new DateTime(
             this.GetYear()
            ,this.GetMonth()
            ,this.GetDay()
            ,this.GetHour()
            ,this.GetMinute()
            ,this.GetSecond()
            ,this.GetMillisecond());
    }
    //返回一個值,該值指示此實例是否與指定的 DateTime 實例相等。
    this.Equals = function(other){
        return this.CompareTo(other) == 0;
    }
    //獲取此實例的日期部分。
    this.GetDate = function(){
        var result = new DateTime(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0);
        return result ;
    }
    //獲取此實例所表示的日期為該月中的第幾天。
    this.GetDay = function(){
        return d.getDate();
    }
    //獲取此實例所表示的日期是星期幾。
    this.GetDayOfWeek = function(){
        return d.getDay();
    }
    //獲取此實例所表示日期的小時部分。
    this.GetHour = function(){
        return d.getHours();
    }
    //獲取此實例所表示日期的分鐘部分。
    this.GetMinute = function(){
        return d.getMinutes();
    }
    //獲取此實例所表示日期的毫秒部分。
    this.GetMillisecond = function(){
        return d.getMilliseconds();
    }
    //獲取此實例所表示日期的月份部分。
    this.GetMonth = function(){
        return d.getMonth() + 1;
    }
    //獲取此實例的下個月一日的DateTime對象
    this.GetNextMonthFirstDay = function(){
        var result = new DateTime(this.GetYear(), this.GetMonth(), 1, 0, 0, 0, 0);
        result = result.AddMonths(1);
        return result;
    }
    //獲取此實例的下一個周日的DateTime對象
    this.GetNextWeekFirstDay = function(){
        var result = this.GetDate();
        return result.AddDays(7 - result.GetDayOfWeek());
    }
    //獲取此實例的下一個周日的DateTime對象
    this.GetNextYearFirstDay = function(){
        return new DateTime(this.GetYear() + 1, 1, 1, 0, 0, 0, 0);
    }
    //獲取此實例所表示日期的秒部分。
    this.GetSecond = function(){
        return d.getSeconds();
    }
    //返回此實例的Date值
    this.GetValue = function(){
        return d;
    }
    //獲取此實例所表示日期的年份部分。
    this.GetYear = function(){
        return d.getFullYear();
    }
    //指示此實例是否是DateTime對象
    this.IsDateTime = function(){}
    //將當前 DateTime 對象的值轉(zhuǎn)換為其等效的短日期字符串表示形式。
    this.ToShortDateString = function(){
        var result = "";
        result = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
        return result;    
    }
    //將當前 DateTime 對象的值轉(zhuǎn)換為其等效的短時間字符串表示形式。
    this.ToShortTimeString = function(){
        var result = "";
        result = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        return result;    
    }
    //將當前 DateTime 對象的值轉(zhuǎn)換為其等效的字符串表示形式。
    this.ToString = function(format){
        if(typeof(format) == "string"){

        }
        return this.ToShortDateString() + " " + this.ToShortTimeString();
    }
    //驗證Add系列的方法參數(shù)是否合法
    function ValidateAddMethodParam(param){
        if(typeof(param) != "number"){
            return false;
        }
        return true;
    }
    //繼承自Date的方法
    this.getTime = function(){
        return d.getTime();
    }
}

//比較 DateTime 的兩個實例,并返回它們相對值的指示。
DateTime.Compare = function(d1, d2){
    return d1.CompareTo(d2);
}
//返回指定年和月中的天數(shù)。
DateTime.DaysInMonth = function(year, month){
if ((month < 1) || (month > 12))
{
return "月份[" + month + "]超出范圍";
}
var numArray = DateTime.IsLeapYear(year) ? DateTime.DaysToMonth366 : DateTime.DaysToMonth365;
return (numArray[month] - numArray[month - 1]);
}
//返回一個值,該值指示 DateTime 的兩個實例是否相等。
DateTime.Equals = function(d1, d2){
    return d1.CompareTo(d2) == 0;
}
//返回指定的年份是否為閏年的指示。
DateTime.IsLeapYear = function(year)
{
if ((year < 1) || (year > 0x270f))
{
return "年份[" + year + "]超出范圍";
}
if ((year % 4) != 0)
{
return false;
}
if ((year % 100) == 0)
{
return ((year % 400) == 0);
}
return true;
}
//獲取一個 DateTime 對象,該對象設(shè)置為此計算機上的當前日期和時間,表示為本地時間。
DateTime.Now = new DateTime();
//將日期和時間的指定字符串表示形式轉(zhuǎn)換為其等效的 DateTime。
DateTime.Parse = function(s){
    var result = new DateTime();
    var value = result.GetValue();
    value.setHours(0,0,0,0);
    var dateRex = //b[1-2][0-9][0-9][0-9][-]/d{1,2}[-]/d{1,2}/b/i;
    if(dateRex.test(s)){
        var dateStr = s.match(dateRex)[0];
        try{
            var dateParts = dateStr.split("-");
            var year = dateParts[0] - 0;
            var month = dateParts[1] - 1;
            var day = dateParts[2] - 0;
            value.setFullYear(year,month,day);
        }catch(ex){
            return null;
        }
        var timeRex = //b/d{1,2}[:]/d{1,2}[:]/d{1,2}/b/i;
        if(timeRex.test(s)){
            var timeStr = s.match(timeRex)[0];
            try{
                var timeParts = timeStr.split(":");
                var hour = timeParts[0] - 0;
                var min = timeParts[1] - 0;
                var sec = timeParts[2] - 0;
                value.setHours(hour,min,sec);
            }catch(ex){

            }
        }
    }else{
        return null;
    }
    return result;
}
//獲取當前日期,其時間組成部分設(shè)置為 00:00:00。
DateTime.Today = new DateTime(null, null, null, 0, 0, 0, 0);

//靜態(tài)字段
DateTime.DaysToMonth365 = [ 0, 0x1f, 0x3b, 90, 120, 0x97, 0xb5, 0xd4, 0xf3, 0x111, 0x130, 0x14e, 0x16d ];
DateTime.DaysToMonth366 = [ 0, 0x1f, 60, 0x5b, 0x79, 0x98, 0xb6, 0xd5, 0xf4, 0x112, 0x131, 0x14f, 0x16e ];
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新龙县| 峨山| 临泽县| 白城市| 赞皇县| 汕头市| 镇远县| 来凤县| 格尔木市| 会昌县| 江安县| 郧西县| 柳河县| 临洮县| 宝应县| 凯里市| 长阳| 隆化县| 聂拉木县| 金华市| 鞍山市| 肇源县| 铁力市| 府谷县| 阿瓦提县| 临夏市| 义马市| 沈阳市| 金溪县| 柞水县| 无棣县| 和静县| 许昌县| 雷山县| 长阳| 万山特区| 玉门市| 南阳市| 防城港市| 台湾省| 台山市|