Date使用

鸡肋文章,方便自己复制粘贴。

1.数据库中to_char() to_date()。
informix:to_date(‘2017-11-28 00:50:00’,’%Y-%m-%d %H:%M:%S’)
oracle:to_date(‘2017-12-28 00:00:00’, ‘YYYY-MM-DD HH24:MI:SS’)

2.java中 Date和SimpleDateFormat

3.js中Date使用
通过当前日期计算几天前或几天后的日期
dayNum=0 返回当前日期
dayNum=1 返回昨天的日期
dayNum=-1 返回明天的日期
返回日期格式:2017-12-29

function getNowFormatDate(dayNum) {
        var date = new Date();
        date.setTime(date.getTime()-24*60*60*1000*dayNum);
        var seperator1 = "-";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        return currentdate;
    }

注意:不能用date.getDate()加减天数来算日期,会超界。

输入XXXX-XX判断XXXX年XX月的起始日期和结束日期
输入:2017-12
返回:
startDate 2017-12-01 00:00:00
endTime 2017-12-31 23:59:59

var time=$("#startTime1").val();
var year=time.substring(0,4);
var month=time.substring(5,7);
if(month=='01'||month=='03'||month=='05'||month=='07'||month=='08'||month=='10'||month=='12'){
    startDate=time+"-01 00:00:00";
    endDate=time+"-31 23:59:59";
}
if(month=='04'||month=='06'||month=='09'||month=='11'){
    startDate=time+"-01 00:00:00";
    endDate=time+"-30 23:59:59";
}
if(month=='02'){
    if(((year%400==0) || (year%100!=0) ) && (year%4==0)){
        startDate=time+"-01 00:00:00";
        endDate=time+"-29 23:59:59";
    }else {
        startDate=time+"-01 00:00:00";
        endDate=time+"-28 23:59:59";
    }
}

重点在于闰年平年判断,大月小月判断 闰年2月29天

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值