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

2017-02-14 17:32:15

2014-06-19 11:57 by 轩脉刃, 136042 阅读, 4 评论, 收藏编辑

复制代码

<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>

复制代码

后面一种直接是设置prototype来做格式的转换。


  • 2018-04-19 16:31:03

    mysql双机热备的实现

    准备两个mysql,A和B,A为主,B为从。前提是这两个数据库现在的表结构要一模一样,否则不成功。这个要锁表处理了。

  • 2018-04-19 16:32:47

    mysql binlog_do_db参数设置的坑

    在配置文件中想当然地配置成binlog_do_db=test,xx,jj,以为是三个库。结果无论什么操作都没有binlog产生

  • 2018-04-20 02:11:58

    Android中finish掉其它的Activity

    在Android开发时,一般情况下我们如果需要关掉当前Activity非常容易,只需要一行代码 this.finish;即可。 那么,如果是想要在当前Activity中关掉其它的Activity应该怎么实现呢? 比如更改了某个设定,程序需要重新运行并加载新的配置文件,就要用到restart或finish让程序重启。

  • 2018-04-20 09:12:07

    如何在 7 分钟内黑掉 40 家网站?

    去年夏天我开始学习信息安全与黑客技术。在过去的一年中,我通过参加各种战争游戏、夺旗以及渗透测试模拟,不断提高我的黑客技术,还学习了很多关于“如何让计算机偏离其预期行为”的新技术。

  • 2018-04-25 00:46:48

    Android开发笔记——SharedPreferences 存储实体类以及任意类型

    我们常常要用到保存数据,Android中常用的存储方式有SQLite,sharedPreferences 等,当然也有各自的应用场景,前者适用于保存较多数据的情形,后者责倾向于保存用户偏好设置比如某个checkbox的选择状态,用户登录的状态等等,都是以键值对的形式进行的文件读取,可以存储String,int,booean等一些基本数据类型等等。

  • 2018-04-25 11:48:44

    Java泛型详解

    泛型是Java中一个非常重要的知识点,在Java集合类框架中泛型被广泛应用。本文我们将从零开始来看一下Java泛型的设计,将会涉及到通配符处理,以及让人苦恼的类型擦除。

  • 2018-05-05 20:31:52

    StringUtils就这1张图,必备(二)

    StringUtils是工作中使用最频繁的一个工具类,提供了大量丰富的字符串操作方法,下面是所有方法的一个蓝图:

  • 2018-05-06 00:41:36

    设置EditText不自动聚焦

    如果界面中有EditText的时候,用户打开界面的话EditText就会自动聚焦。如果想取消这种一打开界面EditText就聚焦效果,可在EditText的上级父容器中加入如下代码: