<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时间戳和日期字符串相互转换的更多相关文章

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

    转:javascript时间戳和日期字符串相互转换 <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...

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

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

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

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

  4. js时间戳与日期格式之间相互转换

    ###js时间戳与日期格式之间相互转换 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法, ...

  5. js 时间戳转为日期格式

    原文:js 时间戳转为日期格式 js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time) ...

  6. js时间戳转换日期

    //js时间戳转换日期function formatDate(now) { var year=now.getFullYear(); var month=now.getMonth()+1; var da ...

  7. libs/tools.js stringToDate dateToString 日期字符串转换函数

    libs/tools.js stringToDate dateToString 日期字符串转换函数 import { stringToDate } from '@/libs/tools.js'   e ...

  8. js时间戳转为日期函数

    js时间戳转为日期函数 function add0(m){ return m<10?'0'+m:m; } //timestamp参数示例:1501234567 function format(t ...

  9. js时间戳与日期格式的相互转换

    下面总结一下js中时间戳与日期格式的相互转换: 1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(ti ...

随机推荐

  1. SpringMVC基础-@RequestMapping

    @RequestMapping @RequestMapping是一个用来处理请求地址映射的注解 可用于类或方法上.   类定义处:提供初步的请求映射信息.相当于当前 WEB 应用的根目录   方法处: ...

  2. Composer简介及使用实例

    1.PHP-FIG 官网:http://www.php-fig.org/ php编码规范: 本文档是PHP互操作性框架制定小组(PHP-FIG :PHP Framework Interoperabil ...

  3. Sass之Compass学习笔记

    compass Compass是Sass的工具库,就好像jQuery是js的库一样. sass有了compass的配合,就会更加事半功倍. Sass本身只是一个编译器,Compass在它的基础上,封装 ...

  4. 关于我立牌坊那个SSM项目

    我这段时间有在写,但是我发现一个问题,就是我经常在做后面功能的时候要改前面一个东西,但是我博客已经发出来了,这让我很头疼.毕竟我博客基本都在纯贴代码. 所以决定暂时停更这个系列.等我写好了再上传到gi ...

  5. java创建目录与文件

    java创建目录与文件: package com.led.file; import java.io.File; import java.io.IOException; public class Fil ...

  6. css 禁止长按保存功能

    *{-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;} 或者指定某个元 ...

  7. Spring之声明式事务

    在讲声明式事务之前,先回顾一下基本的编程式事务 编程式事务: //1.获取Connection对象 Connection conn = JDBCUtils.getConnection(); try { ...

  8. firebird常用语句

    分页写法小例 SELECT FIRST templateid,code,name FROM template ; SELECT FIRST SKIP templateid,code,name FROM ...

  9. 工具类:将其他编码类型转换成UTF-8或者其他类型的工具类

    将其他编码类型转换成UTF-8或者其他类型的工具类 public static String changeUTF(String str) { String newStr = null; try { n ...

  10. Ajax异步请求模板

    $.ajax({ url: '', type: 'post', data: {'id':id}, dataType: 'json', success: function(data,statusText ...