timestamp.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * 时间戳转换工具
  3. * @author zhaoxianlie
  4. */
  5. var Timestamp = (function(){
  6. "use strict";
  7. var intervalId = 0;
  8. var _bindEvents = function(){
  9. $('#btnStampToLocale').click(function(e) {
  10. var stamp = $.trim($('#txtSrcStamp').val());
  11. if(stamp.length == 0) {
  12. alert('请先填写你需要转换的Unix时间戳');
  13. return;
  14. }
  15. if(!parseInt(stamp,10)) {
  16. alert('请输入合法的Unix时间戳');
  17. return;
  18. }
  19. $('#txtDesDate').val((new Date(parseInt(stamp,10) * 1000)).format('yyyy-MM-dd HH:mm:ss'));
  20. });
  21. $('#btnLocaleToStamp').click(function(e) {
  22. var locale = $.trim($('#txtLocale').val());
  23. locale = Date.parse(locale);
  24. if(isNaN(locale)) {
  25. alert('请输入合法的时间格式,如:2014-04-01 10:01:01,或:2014-01-01');
  26. }
  27. $('#txtDesStamp').val(locale / 1000);
  28. });
  29. $('#btnToggle').click(function(e){
  30. var model = $(this).data('model') || 0;
  31. if(model) {
  32. $(this).data('model',0).val('暂停');
  33. _initNowStamp();
  34. }else{
  35. $(this).data('model',1).val('开始');
  36. window.clearInterval(intervalId);
  37. }
  38. });
  39. };
  40. var _initNowStamp = function(){
  41. var txtNowDate = $('#txtNowDate');
  42. var txtNowStamp = $('#txtNow');
  43. intervalId = window.setInterval(function(){
  44. txtNowDate.val((new Date()).toLocaleString());
  45. txtNowStamp.val(Math.round((new Date()).getTime() / 1000));
  46. },1000);
  47. };
  48. var _init = function(){
  49. $(function(){
  50. _initNowStamp();
  51. _bindEvents();
  52. $('#tab0_url').focus();
  53. });
  54. };
  55. return {
  56. init : _init
  57. };
  58. })();
  59. Timestamp.init();