datachange.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function($) {
  2. $.extend({
  3. // console.log($.DateChange.DateToUnix('2014-5-15 20:20:20'));
  4. // console.log($.DateChange.UnixToDate(1325347200));
  5. DateChange: {
  6. /**
  7. * 当前时间戳
  8. * @return <int> unix时间戳(秒)
  9. * @author chanjunkai
  10. */
  11. CurTime: function(){
  12. return Date.parse(new Date())/1000;
  13. },
  14. /**
  15. * 日期 转换为 Unix时间戳
  16. * @param <string> 2014-01-01 20:20:20 日期格式
  17. * @return <int> unix时间戳(秒)
  18. */
  19. DateToUnix: function(string) {
  20. var f = string.split(' ', 2);
  21. var d = (f[0] ? f[0] : '').split('-', 3);
  22. var t = (f[1] ? f[1] : '').split(':', 3);
  23. return (new Date(
  24. parseInt(d[0], 10) || null,
  25. (parseInt(d[1], 10) || 1) - 1,
  26. parseInt(d[2], 10) || null,
  27. parseInt(t[0], 10) || null,
  28. parseInt(t[1], 10) || null,
  29. parseInt(t[2], 10) || null
  30. )).getTime() / 1000;
  31. },
  32. /**
  33. * 时间戳转换日期
  34. * @param <int> unixTime 待时间戳(秒)
  35. * @param <bool> isFull 返回完整时间(Y-m-d 或者 Y-m-d H:i:s)
  36. * @param <int> timeZone 时区
  37. */
  38. UnixToDate: function(unixTime, isFull, timeZone) {
  39. if (typeof (timeZone) == 'number')
  40. {
  41. unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
  42. }
  43. var time = new Date(unixTime * 1000);
  44. var ymdhis = "";
  45. ymdhis += time.getUTCFullYear() + "-";
  46. ymdhis += (time.getUTCMonth()+1) + "-";
  47. ymdhis += time.getUTCDate();
  48. if (isFull === true)
  49. {
  50. ymdhis += " " + time.getUTCHours() + ":";
  51. ymdhis += time.getUTCMinutes() + ":";
  52. ymdhis += time.getUTCSeconds();
  53. }
  54. return ymdhis;
  55. }
  56. }
  57. });
  58. })(jQuery);