zoukankan      html  css  js  c++  java
  • js【输入一个日期】返回【当前12个月每月最后一天】

    Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
    }
    //获得某月的最后一天
    function getLastDay(year, month) {
    var new_year = year; //取当前的年份
    var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
    if (month > 12) {
    new_month -= 12; //月份减
    new_year++; //年份增
    }
    var new_date = new Date(new_year, new_month, 1); //取当年当月中的第一天
    return new Date(new_date.getTime() - 1000 * 60 * 60 * 24).Format('yyyy-MM-dd');//获取当月最后一天日期
    }
    function getLastDayForN(y, m, n) {
    if (n > 0) {
    var yy = m === 1 ? y - 1 : y;
    var mm = m - 1 || 12;
    getLastDayForN(yy, mm, n - 1);
    }
    var currentDate = getLastDay(y, m);
    console.log(currentDate);
    }
    getLastDayForN(2016,5,12);

  • 相关阅读:
    元类
    多态及多态性和鸭子类型
    类与对象和对应方法、封装
    面向对象与类
    内存管理与正则(re)模块
    Ajax
    Microsoft SQL Server2008安装教程
    兼容所有浏览器的关闭当前页面方法
    如何快速找到指定端口被哪个程序占用并释放该端口(解决bindException)
    MSSQL
  • 原文地址:https://www.cnblogs.com/wu-peng/p/5514928.html
Copyright © 2011-2022 走看看