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

<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;
//当前时间戳为:1403149534
console.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.000Z
console.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 GMT
console.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>

转:javascript时间戳和日期字符串相互转换的更多相关文章

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

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  2. java 时间戳与日期字符串相互转换

    /** * 时间戳转换成日期格式字符串 * @param seconds 精确到秒的字符串 * @param formatStr * @return */ public static String t ...

  3. js时间戳和日期字符串相互转换

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  4. javascript时间戳与日期格式的相互转换

    这里总结下JavaScript中时间戳和日期格式的相互转换方法(自定义函数). 将时间戳转换为日期格式 function timestampToTime(timestamp) { var date = ...

  5. python获取本地时间,时间戳与日期格式相互转换

    附上代码与运行结果截图: import time # 获取当前时间 now = time.localtime() # 格式化日期 now_ = time.strftime('%Y-%m-%d %H:% ...

  6. Javascript时间戳和日期时间的相互转换

    跟后台对接的时候经常碰到时间格式的问题,有时返回的是时间戳,有时返回的是具体时间,需求又需要它们之间的转换,所以干脆把之前遇到过的情况都给记录下来,以供自己参考! 本文备注:(时间戳单位为毫秒ms,换 ...

  7. Go时间戳和日期字符串的相互转换

    Go语言中,获取时间戳用time.Now().Unix(),格式化时间用t.Format,解析时间用time.Parse. 看实例代码: package main import ( "fmt ...

  8. javascript时间戳与日期格式之间的互转

    1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 ...

  9. javascript的对象与字符串相互转换

    因为对象不利于网络传输,因此要转换成字符串,转换成字符串之后又要考虑怎么将这个字符串转换回对象,以便取得对象中的属性. 常用的做法是将对象转换为JSON字符串,这里的转换方法也是用的JSON官方提供的 ...

随机推荐

  1. http 缓存策略浅析

    从一道经典的面试题说起 "用户输入 URL 到浏览器显示页面,这个过程发生了什么?",作为前端开发,这个题目相信大家并不陌生.楼主的答案分为两部: 一.网络通信 应用层 DNS 域 ...

  2. python实战——网络爬虫之request

    Urllib库是python中的一个功能强大的,用于操做URL,并在做爬虫的时候经常要用到的库,在python2中,分为Urllib和Urllib2两个库,在python3之后就将两个库合并到Urll ...

  3. Supporting Right-to-Left Languages

    For the most part iOS supports Right-to-Left (RTL) languages such as Arabic with minimal developer e ...

  4. Windows10下简单搭建zookeeper

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6798669.html 1 简介 zookeeper是Apache的一个开源项目,致力于开发和维护一个开源的服务器 ...

  5. ActiveRecord::Fixture::FixtureError: table "users" has no column named "activated_at".

    window 7+ruby2.33+rails5.0. 在测试的时候 rails test 报固件fixture错误: 没有某列字段存在 虽然可以直接通过开发框架去修改字段,但是开发过程中应该通过迁移 ...

  6. php生成markdown格式的数据库字典

    <?php /** * 生成mysql数据字典 */ //数据库配置 $config = [ 'host' => '192.168.43.134', 'user' => 'root' ...

  7. implements和extends的区别

    extends可以理解为全盘继承了父类的功能 implements可以理解为为这个类附加一些额外的功能 举个例子,Animal是一个父类,cat,dog,bird,insect都extends了Ani ...

  8. Android4.0以上版本比较靠谱的获取手机屏幕分辨率的方法

    /** * 获取虚拟按键栏高度 * * @param context * @return */ public static int getNavigationBarHeight(Context con ...

  9. 开发工具 -- PyDev 在 Eclipse中的安装

    1. 将从sorceforge下载到的PyDev3.4.1解压后放到eclipse的插件目录下F:\APP\IDE\Java\Eclipse\eclipse-java-kepler-SR2-win32 ...

  10. PostgreSQL执行超时的问题

    PostgreSQL执行超时的问题 使用背景 最近在使用PostgreSQL的时候,在执行一些数据库事务的时候,先后出现了statement timetout 和idle-in-transaction ...