Ruby's Louvre

每天学习一点点算法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Date对象的一些相关函数

传入两个Date类型的日期,求出它们相隔多少天。

var getDatePeriod = function(start,finish){
     return Math.abs(start*1-finish*1)/60/60/1000/24;
}

传入一个Date类型的日期,求出它所在月的第一天。

var getFirstDateInMonth= function(date){
     return new Date(date.getFullYear(),date.getMonth(),1);
}

传入一个Date类型的日期,求出它所在月的最后一天。

var getLastDateInMonth= function(date){
     return new Date(date.getFullYear(),date.getMonth()+1,0);
}

传入一个Date类型的日期,求出它所在季度的第一天。

var getFirstDateInQuarter= function(date){
     return new Date(date.getFullYear(),~~(date.getMonth()/3)*3,1);
}

传入一个Date类型的日期,求出它所在季度的最后一天。

var getFirstDateInQuarter= function(date){
     return new Date(date.getFullYear(),~~(date.getMonth()/3)*3+3,0);
}

判断是否为闰年。

Date.prototype.isLeapYear = function()   { 
      return new Date(this.getFullYear(),2,0).getDate() == 29;
}

取得当前周某星期几的Date对象,参数为星期号(星期一为1,星期二为2……星期日为7)。

var getWeekDay = function(num){
   var now = new Date(new Date.setHours(0,0,0,0));
   var day = now.getDay() || 7;
   var delta = Math.abs(day-num) * 60 * 60 * 1000 * 24;
   if(day > num){
     return new Date(now - delta);
   }else if(day < num){
     return new Date(now + delta);
   }else{
     return now;
   }
}
// alert(getWeekDay(1).toLocaleDateString())
//2011.5.30更新
      function daysInMonth ( year, month ) {
        var num = new Date();
        num.setFullYear( year, (month == 12) ? 1 : month + 1, 0 );
        return num.getDate();
      }
 
      function dateDif( inDate1, inDate2 ) {
        var ret = new Array();
        var date1 = new Date( (inDate1 <= inDate2) ? inDate1 : inDate2 );
        var date2 = new Date( (inDate1 <= inDate2) ? inDate2 : inDate1 );
        var temp1 = new Date( date2.getFullYear(), date1.getMonth(), date1.getDate() );
        var temp2 = new Date( date2.getFullYear(), date2.getMonth() - (date1.getDate() > date2.getDate() ? 1 : 0 ), date1.getDate() );
        /*Y*/ ret[0] = date2.getFullYear() - date1.getFullYear() - (date2 < temp1 ? 1 : 0);
        /*M*/ret[1] = temp2.getMonth() - temp1.getMonth() + (temp1.getMonth() > temp2.getMonth() ? 12 : 0 );
        /*D*/ret[2] = date2.getDate() - temp2.getDate() + (temp2.getDate() > date2.getDate() ? daysInMonth(temp2.getFullYear(), temp2.getMonth()) : 0);
        return ret;
      }
//2011.5.30更新
function getDaysInMonth1(date) {
    switch (date.getMonth()) {
    case 0:
    case 2:
    case 4:
    case 6:
    case 7:
    case 9:
    case 11:
        return 31;
    case 1:
        var y = date.getFullYear();
        return y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ? 29 : 28;
    default:
        return 30;
    }
}
 
var getDaysInMonth2 = (function() {
    var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
 
    function isLeapYear(date) {
        var y = date.getFullYear();
        return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
    }
    return function(date) { // return a closure for efficiency
        var m = date.getMonth();
 
        return m == 1 && isLeapYear(date) ? 29 : daysInMonth[m];
    };
})();
 
function getDaysInMonth3(date) {
    return new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
}
 
function test(f) {
    var d = new Date;
    var date = new Date(2000, 1, 2);
    for (var i = 0; i < 80000; i++)
        f(date);
    return(new Date - d);
}
function test2(f) {
    var d = new Date;
    for (var i = 0; i < 80000; i++)
        f(d);
    return(new Date - d);
}
alert([test(getDaysInMonth1), test(getDaysInMonth2), test(getDaysInMonth3)]);
alert([test2(getDaysInMonth1), test2(getDaysInMonth2), test2(getDaysInMonth3)]);

判定字符串2009/2/23(或者2009/02/23)是否合法

var test = function(str){
   var arr = str.split("/")
   var m = Number(arr[1])-1
   var d = new Date(Number(arr[0]),m,Number(arr[2]) )
   if(isNaN(d))
     return false;
   if(isNaN(d.getDate()))
     return false;
   return d.getMonth() === m
}
test("2009/2/25")//true
test("2009/2/29")//false
test("2009/2/32")//false
test("2009/02/15")//true
//附上abcd的
function test(s){
    return /^(\d{4})\/(\d\d?)\/(\d\d?)$/.test(s) && s.replace(/^(\d{4})\/(\d\d?)\/(\d\d?)$/,function(a,b,c,d){
        var m = parseInt(c,10);
        if (m < 1 || m > 12) return false;
        var d = parseInt(d,10);
        return d > 0 && d <= (
            m == 2 ? (m = parseInt(b, 10), m % 4 == 0 && m % 100 != 0 || m % 400 == 0) ? 29 : 28 :
            m == 4 || m == 6 || m == 9 || m == 11 ? 30 : 31);
    });
}

如果您觉得此文有帮助,可以打赏点钱给我支付宝1669866773@qq.com ,或扫描二维码

posted on   司徒正美  阅读(1691)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2009-09-16 数组取最大值与最小值
2009-09-16 javascript的缓动效果(第1部分)
点击右上角即可分享
微信分享提示