JS 时间格式 相互转化
1. 时间字符串格式
var dateString1 = '2016-06-15 10:22:00';
var dateString2 = '2016/06/15 10:22:00';
var dateString3 = '2016 06 15 10:22:00';
2. 中国标准时间
var date1 = new Date(); // 获取当前时间,格式为中国标准时间
var date2 = new Date(dateString1); // 将时间字符串转化为对应的中国标准时间
var date3 = new Date(时间戳); // 将时间戳转化为对应的中国标准时间
3. 时间戳 - 通过将中国标准时间转化而成
var timestamp1 = Date.parse(date1/date2); // 获取的时间戳是把毫秒改成000显示
var timestamp2 = (date1/date2).valueOf(); // 获取了当前毫秒的时间戳
var timestamp3 = (date1/date2).getTime(); // 获取了当前毫秒的时间戳
4. 获取自定义格式的日期
// 1. 若小于10,前面加0
function isZero(m){
return m<10?'0'+m:m
}
// 将字符串转为Date格式,获取对应的年、月、日、时、分、秒。组合格式
function formatDate(shijianchuo) {
//时间戳是整数,否则要parseInt转换
var time = new Date(shijianchuo); // 需要使用Date格式进行日期转化,若是时间戳、字符串时间,需要通过new Date(..)转化
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'/'+isZero(m)+'/'+isZero(d)+' '+isZero(h)+':'+isZero(mm)+':'+isZero(s);
}
// 调用 -- 输出: 2019/01/01 10:00:00
formatDate('2019 01 01 10:00:00');
JS 时间格式 相互转化的更多相关文章
- JS时间格式CST转GMT
转换方法:JS 时间格式CST转GMT 时区和Date:Java中的Date和时区转换
- js 时间格式转换
js时间格式转换 格式化时间转成时间戳 //格式化转时间戳(单位秒) function strtotime(strtime) { strtime = strtime.substring(0, 19); ...
- moment.js 时间格式转换
moment.js 时间格式转换 moment.js 时间转化 bug 格式错误 bug 02:00 => 14:00 format HH 与 hh HH === 24 小时制 hh === 1 ...
- JS时间格式 GMT格式转换
JavaScript时间格式转换总结 1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateS ...
- JS 时间格式CST转GMT
近几天,在做百度地图时,需要转换时间格式并做显示,但是发现显示的时间格式,出现了错乱,二者的日期和小时都出现了变动.例如: 原始时间格式:Thu Aug 18 20:38:54 CST 2016 转换 ...
- JS时间格式和时间戳的相互转换
时间戳转化为日期的方式 ; var newDate = new Date(); newDate.setTime(timestamp * ); // Mon May 28 2018 console.lo ...
- JS时间格式和时间戳的互转
//时间格式转为时间戳 function sjc(){ var date = new Date(); //时间对象 var str = date.getTime(); //转换成时间戳 } //时间戳 ...
- js ---- 时间格式
Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- js时间格式转换
在javascript中直接输出Date得到的结果是这样的: function date(){ var date = new Date(); alert(date); } 结果是:Mon Jun 15 ...
随机推荐
- Chrome 开发工具之 Application
Chrome 开发者工具有 Application 这么一个面板,主要作用是检查 web 应用加载的所有资源,包括 Manifest.Service Workers.Local Storage.Ses ...
- C#开发BIMFACE系列13 服务端API之获取转换状态
系列目录 [已更新最新开发文章,点击查看详细] 在<C#开发BIMFACE系列12 服务端API之文件转换>中详细介绍了7种文件转换的方法.发起源文件/模型转换后,转换过程可能成功 ...
- GIS基础知识 - 坐标系、投影、EPSG:4326、EPSG:3857
最近接手一个GIS项目,需要用到 PostGIS,GeoServer,OpenLayers 等工具组件,遇到一堆地理信息相关的术语名词,在这里做一个总结. 1. 大地测量学 (Geodesy) 大地测 ...
- [SNOI2019]字符串
名称:字符串 来源:2019年陕西省选 题目内容 传送门 洛谷(P5392) 题目描述 给出一个长度为$n$的由小写字母组成的字符串$a$,设其中第$i$个字符为$a_i(1≤i≤n)$. 设删掉第$ ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- bzoj 1051 [HAOI2006]受欢迎的牛(tarjan缩点)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1051 题解:缩点之后判断出度为0的有几个,只有一个那么输出那个强连通块的点数,否者 ...
- icpc 江苏 D Persona5 组合数学 大数阶乘(分段阶乘) 大数阶乘模板
Persona5 is a famous video game. In the game, you are going to build relationship with your friends. ...
- 2018湖南多校第二场-20180407 Barareh on Fire
Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...
- HDU 1223 还是畅通工程(最小生成树prim模板)
一个很简单的prim模板,但虽然是模板,但也是最基础的,也要脱离模板熟练打出来 后期会更新kruskal写法 #include<iostream> #include<cstdio&g ...
- 【LeetCode】78-子集
题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [ ...