javascript 时间格式(很方便的原生函数)
原文:http://www.cnblogs.com/yjf512/p/3796229.html
---------------------
<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 时间格式(很方便的原生函数)的更多相关文章
- JavaScript 时间格式
方法1: Date.prototype.Format = function (fmt) { var o = { , //月份 "d+": this.getDate(), //日 = ...
- javascript时间格式转换(今天,昨天,前天)
function transDate() { var $time =document.getElementById("share-time"); var date = $time. ...
- WordPress 博客文章时间格式the_time()设置
国外设计的WordPress 主题里的文章的时间格式是类似“十一月 21, 2010”这种格式的,而中国人习惯的是年在前,月紧跟其后,日在末尾,所以看国外的就显得很别扭,但是我们可以通过修改WP时间代 ...
- JS时间格式 GMT格式转换
JavaScript时间格式转换总结 1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateS ...
- C#里如何简单的校验时间格式
前言: 晚上打算睡觉的时候,群里反馈订单接收失败,开工排查问题,日志显示验签失败,发现一个蛮有意思的BUG,总算有了一个写作的素材 场景描述 本次的场景属于比较常见的收单API,对第三方的订单进行签名 ...
- [php基础]Mysql日期函数:日期时间格式转换函数详解
在PHP网站开发中,Mysql数据库设计中日期时间字段必不可少,由于Mysql日期函数输出的日期格式与PHP日期函数之间的日期格式兼容性不够,这就需要根据网站实际情况使用Mysql或PHP日期转换函数 ...
- JavaScript 时间、格式、转换及Date对象总结
悲剧的遇到问题,从前台得到时间,“Tue Jan 29 16:13:11 UTC+0800 2008”这种格式的,想再后台解析成想要的格式,但是在后台就是解析不了SimpleDateFormat也试着 ...
- sqlserver 时间格式函数详细
一.时间函数 在使用存储过程,sql函数的时候,会遇到一些对时间的处理.比如时间的获取与加减.这里就用到了sql自带的时间函数.下面我列出这些函数,方便日后记忆,使用. --getdate 获取当前时 ...
- MySQL时间戳和时间格式转换函数
MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...
随机推荐
- sql server 中引號嵌套
在SQL字符串是以单引号作为分界符的,在字符串前面和后面各一个单引号.但是字符串中也能包含单引号,为了使语法分析器能够区分字符串中的单引号还是分界符.规定当字符串中出现单引号时,在其前面添加一个单引号 ...
- 自己编辑Nuget拓展包,并发布Nuget服务器,提供下载使用
1. 在NuGet官网上注册并获取API Key 到NuGet上注册一个新的账号,然后在My Account页面,获取一个API Key,如果没有则在API keys 页面创建一个就可以. 2. 下载 ...
- jquery /css3 全屏的渐变背景
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql group_concat函数详解
group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ) 1. --以id分组,把price字 ...
- getDate() 获取时间 如2018年09月21日 11:32:11
function p(s) { return s < 10 ? '0' + s: s;} function getDate() { var myDate = new Date(); //获取当前 ...
- struts2与常用表格ajax操作的json传值问题
struts与常用的dataTables和jqueryGrid等表格进行ajax传值时,经常会传值不适配的问题,这是因为struts在进行ajax操作时已经对你要操作的json数据进行了处理,所以不需 ...
- 【2019 1月集训 Day1】回文的后缀
题意: 给定 n,s,求有多少个字符集大小为 s ,长度为 n 的字符串,使得其不存在一个长度大于 1 的回文后缀. 答案对 m 取模. 分析: 考场见到计数题的链式反应,想写暴力—>暴力难写— ...
- buf.values()
buf.values() 返回:{Iterator} 创建并返回一个包含 Buffer 值(字节)的迭代器.当 Buffer 使用 for..of 声明时将自动调用该函数. const buf = B ...
- 一个简单的模板了解css+div网页布局
直接附上最终效果图: index.html内容: <html> <!--20170730 soulsjie--> <head> <meta http-equi ...
- CMM
CMM CMM的基本概念 CMM(Capability Maturity Model for Software) 它是对于软件组织在定义,实施,度量,控制和改善其软件过程的实践中各个发展阶段的描述.其 ...