javascript时间戳和日期字符串相互转换

2017-02-14 17:30:53
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">// 获取当前时间戳(以s为单位)var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;//当前时间戳为:1403149534console.log("当前时间戳为:" + timestamp);// 获取某个时间格式的时间戳var stringTime = "2014-07-10 10:21:12";var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;//2014-07-10 10:21:12的时间戳为:1404958872 console.log(stringTime + "的时间戳为:" + timestamp2);// 将当前时间换成时间格式字符串var timestamp3 = 1403058804;var newDate = new Date();
newDate.setTime(timestamp3 * 1000);// Wed Jun 18 2014 console.log(newDate.toDateString());// Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toGMTString());// 2014-06-18T02:33:24.000Zconsole.log(newDate.toISOString());// 2014-06-18T02:33:24.000Z console.log(newDate.toJSON());// 2014年6月18日 console.log(newDate.toLocaleDateString());// 2014年6月18日 上午10:33:24 console.log(newDate.toLocaleString());// 上午10:33:24 console.log(newDate.toLocaleTimeString());// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)console.log(newDate.toString());// 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toTimeString());// Wed, 18 Jun 2014 02:33:24 GMTconsole.log(newDate.toUTCString());

Date.prototype.format = function(format) {       var date = {              "M+": this.getMonth() + 1,              "d+": this.getDate(),              "h+": this.getHours(),              "m+": this.getMinutes(),              "s+": this.getSeconds(),              "q+": Math.floor((this.getMonth() + 3) / 3),              "S+": this.getMilliseconds()
       };       if (/(y+)/i.test(format)) {
              format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
       }       for (var k in date) {              if (new RegExp("(" + k + ")").test(format)) {
                     format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
              }
       }       return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));</script>


  • 2017-07-26 11:57:00

    Laravel 定时任务

    在 php 中使用定时器是一件不太简单的事情,之前大概只能通过 cron 来实现定时任务。但是在 Laravel5 中,定时任务将会变得很简单。

  • 2017-08-03 21:16:46

    Node.js 里面那些遗失的 ES6 特性

    其实 Node.js 对 ES6 的很多特性都已经开始支持了。 在 Node.js 使用的 JS 引擎 V8 里面将不同状态 ES6 特性分成了 3 个等级:

  • 2017-08-08 11:17:17

    nginx 反向代理 取得真实IP和域名

    nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递到应用程序中。

  • 2017-08-09 15:14:52

    如何写好.babelrc?Babel的presets和plugins配置解析

    官网是这么说的,翻译一下就是下一代JavaScript 语法的编译器。 作为前端开发,由于浏览器的版本和兼容性问题,很多JavaScript的新的方法都不能使用,等到可以大胆使用的时候,可能已经过去了好几年。Babel就因此而生,它可以让你放心使用大部分的JavaScript的新的标准的方法,然后编译成兼容绝大多数的主流浏览器的代码。