码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript一些格式化日期方法

时间:2015-07-04 11:21:10      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:javascript

这些方法都是在做项目的时候写的,有些可能写的不好,但是收集起来可以做为一个日期格式化工具了。


var dateUtils = {
    /**
     * 格式化日期
     * @param date
     * @returns {string}
     */
    getFormatDay: function(date) {
        var dateTime;
        var dateFormat;
        //        var flag=date.indexOf("-");
        if (typeof date == "object") {
            dateTime = new Date(date);
        } else {
            dateTime = new Date(date.replace(/-/g, ‘/‘));
        }
        //--- -将时间格式改为如2014-01-01----
        var mon2 = (Number(dateTime.getMonth()) + 1);
        var day2 = dateTime.getDate();
        if (mon2 < 10) {
            mon2 = "0" + mon2;
        } else {
            mon2 = mon2 + "";
        }
        if (day2 < 10) {
            day2 = "0" + day2;
        } else {
            day2 = day2 + "";
        }
        dateFormat = dateTime.getFullYear() + "-" + mon2 + "-" + day2;
        return dateFormat;
    },
    /**
     * 格式化时间,转为“日+小时+分+秒”
     * @param longTime 单位:秒
     */
    getFormatTime: function(longTime) {
        var time = parseFloat(longTime);
        if (time != null && time != "") {
            if (time < 60) {
                var s = time;
                time = s + "秒";
            } else if (time >= 60 && time < 3600) {
                var m = parseInt(time / 60);
                var s = parseInt(time % 60);
                time = m + "分钟" + s + "秒";
            } else if (time >= 3600 && time < 86400) {
                var h = parseInt(time / 3600);
                var m = parseInt(time % 3600 / 60);
                var s = parseInt(time % 3600 % 60 % 60);
                time = h + "小时" + m + "分钟" + s + "秒";
            } else if (time >= 86400) {
                var d = parseInt(time / 86400);
                var h = parseInt(time % 86400 / 3600);
                var m = parseInt(time % 86400 % 3600 / 60);
                var s = parseInt(time % 86400 % 3600 % 60 % 60);
                time = d + "天" + h + "小时" + m + "分钟" + s + "秒";
            }
        }
        return time;
    },
    /**
     * 获得今天凌晨的时间(秒为单位)
     */
    getTodayTime: function() {
        var today = new Date();
        var strYear = today.getFullYear();
        var strDay = today.getDate();
        var strMonth = today.getMonth() + 1;
        if (strMonth < 10)
            strMonth = "0" + strMonth;
        if (strDay < 10)
            strDay = "0" + strDay;
        var strToday = strYear + "-" + strMonth + "-" + strDay;
        var strTodayTime = strToday + " 00:00:00";

        var todayDate = new Date(strTodayTime.replace(/-/g, ‘/‘));
        return todayDate.getTime();
    },
    /**
     * 获得昨天的日期
     */
    getProxDay: function() {
        var today = new Date();
        var yesterday_milliseconds = today.getTime() - 1000 * 60 * 60 * 24;
        var yesterday = new Date();
        yesterday.setTime(yesterday_milliseconds);

        var strYear = yesterday.getFullYear();
        var strDay = yesterday.getDate();
        var strMonth = yesterday.getMonth() + 1;
        if (strMonth < 10)
            strMonth = "0" + strMonth;
        if (strDay < 10)
            strDay = "0" + strDay;
        var strYesterday = strYear + "-" + strMonth + "-" + strDay;
        var strYesterdayTime = strYesterday + " 00:00:00";

        var proxDate = new Date(strYesterdayTime.replace(/-/g, ‘/‘));
        return proxDate;
    }
};

dateUtils.getProxDay()
Fri Jul 03 2015 00:00:00 GMT+0800 (中国标准时间)
dateUtils.getTodayTime()
1435939200000
dateUtils.getFormatDay(new Date())
“2015-07-04”


版权声明:本文为博主原创文章,未经博主允许不得转载。

JavaScript一些格式化日期方法

标签:javascript

原文地址:http://blog.csdn.net/allgis/article/details/46753403

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!