jquery 时间戳与日期转换

admin9年前技术文档1623
(function($) {
    $.extend({
        myTime: {
            /**
             * 当前时间戳
             * @return <int>        unix时间戳(秒)  
             */
            CurTime: function(){
                return Date.parse(new Date())/1000;
            },
            /**              
             * 日期 转换为 Unix时间戳
             * @param <string> 2014-01-01 20:20:20  日期格式              
             * @return <int>        unix时间戳(秒)              
             */
            DateToUnix: function(string) {
                var f = string.split(' ', 2);
                var d = (f[0] ? f[0] : '').split('-', 3);
                var t = (f[1] ? f[1] : '').split(':', 3);
                return (new Date(
                        parseInt(d[0], 10) || null,
                        (parseInt(d[1], 10) || 1) - 1,
                        parseInt(d[2], 10) || null,
                        parseInt(t[0], 10) || null,
                        parseInt(t[1], 10) || null,
                        parseInt(t[2], 10) || null
                        )).getTime() / 1000;
            },
            /**              
             * 时间戳转换日期              
             * @param <int> unixTime    待时间戳(秒)              
             * @param <bool> isFull    返回完整时间(Y-m-d 或者 Y-m-d H:i:s)              
             * @param <int>  timeZone   时区              
             */
            UnixToDate: function(unixTime, isFull, timeZone) {
                if (typeof (timeZone) == 'number')
                {
                    unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
                }
                var time = new Date(unixTime * 1000);
                var ymdhis = "";
                ymdhis += time.getUTCFullYear() + "-";
                ymdhis += (time.getUTCMonth()+1) + "-";
                ymdhis += time.getUTCDate();
                if (isFull === true)
                {
                    ymdhis += " " + time.getUTCHours() + ":";
                    ymdhis += time.getUTCMinutes() + ":";
                    ymdhis += time.getUTCSeconds();
                }
                return ymdhis;
            }
        }
    });
})(jQuery);

使用方法

console.log($.myTime.DateToUnix('2014-5-15 20:20:20'));
console.log($.myTime.UnixToDate(1325347200));


分享到:

相关文章

Zend Studio 8打开utf-8文件出现乱码解决方法

Zend Studio 8打开utf-8文件出现乱码解决方法

今天安装了zend studio 8.0.1,运行了一个PHP程序项目(项目文件编码为utf-8),发现utf-8文件中的中文全部变成乱码了,后面终于经过寻找探索,找到了问题所 在:虽然你的项目...

浅谈 PHP 与手机 APP 开发(API 接口开发)

文章转载自:http://www.thinkphp.cn/topic/5023.html这个帖子写给不太了解PHP与API开发的人一、先简单回答两个问题:1、PHP 可以开发客户端?答:不可以,因为P...

Git 忽略.idea/workspace.xml文件

git忽略的原理: git设置本地忽略必须保证git的远程仓库分支上没有这个要忽略的文件,如果远程分支上存在这个文件,本地在设置ignore将不起作用。========================...

vue配置iframe父级窗口通信

在router/index.js中配置export default new Router({   scrollBehavior: ()&n...

textarea 中的换行符号

textarea 中的换行符号

在网页中初始化textarea文本时,控制文本的换行需要使用特殊的符号来实现,经过查阅各种资料后终于找到了实现textarea文本换行的符号:&#13;&#10; 例如:&l...

php header 之 Set-Cookie

php header 之 Set-Cookie

php设置cookie可以使用setcookie函数或header函数。使用header方式时候需要注意字符串的顺序,如果顺序不对,可能会出现意想不到的问题。正确的顺序为name=value;&nbs...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。