jquery中實現(xiàn)時間戳與日期相互轉換
更新時間:2016年04月12日 10:57:54 作者:FreeSpider
本文主要利用jquery擴展寫了一個myTime對象,并寫了2個函數(shù)分別處理日期和時間戳之間的相互轉換。
直接看代碼:
提醒:不要忘記了引用jquery的類庫
(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 時區(qū) */ 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);
調(diào)用方法:
復制代碼 代碼如下:
<script>
document.write($.myTime.DateToUnix('2016-04-12 10:49:59')+'<br>');
document.write($.myTime.UnixToDate(1460429399));
</script>
document.write($.myTime.DateToUnix('2016-04-12 10:49:59')+'<br>');
document.write($.myTime.UnixToDate(1460429399));
</script>
相關文章
jquery實現(xiàn)動態(tài)改變css樣式的方法分析
這篇文章主要介紹了jquery實現(xiàn)動態(tài)改變css樣式的方法,結合實例形式分析了jQuery動態(tài)操作css樣式的設置、獲取及應用等相關操作技巧,需要的朋友可以參考下2019-05-05Jquery網(wǎng)頁出現(xiàn)的亂碼問題的三種解決方法
很多時候,在網(wǎng)上下的一些Jquery插件,在頁面運行時出現(xiàn)亂碼問題,我總結了三點,希望對大家有所幫助:2013-06-06AspNet中使用JQuery上傳插件Uploadify詳解
Uploadify是JQuery的一個上傳插件,實現(xiàn)的效果非常不錯,帶進度顯示。不過官方提供的實例時php版本的,本文將詳細介紹Uploadify在Aspnet中的使用2015-05-05jQuery EasyUI API 中文文檔 - Spinner微調(diào)器使用
jQuery EasyUI API 中文文檔 - Spinner微調(diào)器使用,需要的朋友可以參考下。2011-10-10