js 时间时间戳互换
<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>

js 时间时间戳互换的更多相关文章
- js中时间戳转换成时间格式
js中时间戳转换成时间格式, // 时间戳转换成时间格式 var formatDate = function(date){ date = new Date(date); var y=date.getF ...
- JS时间转时间戳,时间戳转时间。时间显示模式。
函数内容 // 时间转为时间戳 function date2timestamp(datetime) { var timestamp = new Date(Date.parse(datetime)); ...
- js格式化时间 js格式化时间戳
一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...
- js时间格式化函数,支持Unix时间戳
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- js时间和时间戳之间如何转换(汇总)
js时间和时间戳之间如何转换(汇总) 一.总结 一句话总结: 1.js中通过new Date()来获取时间对象, 2.这个时间对象可以通过getTime()方法获取时间戳, 3.也可以通过getYea ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
- js 时间处理
1.格式化时间 function GetDateTimeFormatter(value) { if (value == undefined) { return &q ...
- js格式化时间戳
//js格式化时间戳,转换为时间格式 2017-1-15 4:10:15 function getLocalTime(nS) { var time = new Date(parseInt(nS) * ...
- js 时间类函数
js 时间类是 Date() var currtime = new Date();// 实例一个时间,是当前时间 接收一个时间戳为参数 var time2=new Date(currtime.get ...
随机推荐
- Spring注解问题,[action中注入service失败
pring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- Git环境搭建以及上传到GitHub全文记录
1.百度搜索下载git,官网可能很慢,我在百度软件里面下载的.当然可能不是最新版本.一路回车安装就好. 2.设置本机git的用户名和邮箱地址 查看当前电脑是否设置了用户名称 $ git config ...
- @PostConstruct 注解
@PostConstruct 注解 /* * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. ...
- python3.6 简单爬虫
# coding='UTF-8' from bs4 import BeautifulSoup # 引入beautifulsoup 解析html事半功倍 import re import urllib ...
- Screen的简单使用
第1步:新建窗口 输入: screen [-S <作业名称>] 第2步:随便干些什么 在窗口中执行任意命令,干什么都行 第3步:挂起窗口 Ctrl + a d 第4步:还原窗口 scree ...
- zookeeper + dubbo + spring boot
最近开始接触了分布式的一些东西,这里给自己作一个学习笔记. 这里只是做一个运行demo,具体的理论知识就不在这里阐述了. 1.zookeeper的安装与启动 下载地址:http://www.apach ...
- 我的日志文件java logger
操作读取日志文件, 1.使用默认的日志文件,并验证默认级别 public void originalConfig() { Logger logger = Logger.getLogger(Logger ...
- Centos7配置文件共享服务器SAMBA三步曲(转)
1.安装 yum install samba samba-client samba-common -y 2.配置 备份已有配置 mv /etc/samba/smb.conf /etc/samba/sm ...
- 如何查看sql server端口号
通过sql server配置管理器-->sql server网络配置-->选择-->通过右侧选择TCP/IP(已启用)-->查看属性 还可以通过sql语句查看: exec sy ...
- 【转】ActionScript,Flash,Flash/Flex Builder,FlashPlayer,AIR,swf,swc,swz之间的区别
原文链接:http://zengrong.net/post/1295.htm ActionScript ActionScript通常简称为AS,它是Flash平台的语言.AS编写的程序,最终可以编译成 ...