JS时间戳和时间之间转换
一、时间转换时间戳
var date = new Date(); //时间对象
var str = date.getTime(); //转换成时间戳
二、时间戳转换为时间
1.转换成形如 2018年1月4日 14:00:00 格式:
function getDate(date){
var t = new Date(date).toLocaleString();
return t;
}
2.还可以这样
// 也很简单
var strtime = '2014-04-23 18:55:49:123'; var date = new Date(strtime.replace(/-/g, '/')); // 有三种方式获取,在后面会讲到三种方式的区别
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date); /*
三种获取的区别:
第一、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
比如上面代码输出的结果(一眼就能看出区别):
1398250549123
1398250549123
1398250549000
*/
3. 转换成更多其他的格式:
// 简单的一句代码
var date = new Date(时间戳); //获取一个时间对象 /**
1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了
2. 更多好用的方法可以在这查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp
*/
date.getFullYear(); // 获取完整的年份(4位,1970)
date.getMonth(); // 获取月份(0-11,0代表1月,用的时候记得加上1)
date.getDate(); // 获取日(1-31)
date.getTime(); // 获取时间(从1970.1.1开始的毫秒数)
date.getHours(); // 获取小时数(0-23)
date.getMinutes(); // 获取分钟数(0-59)
date.getSeconds(); // 获取秒数(0-59)
JS时间戳和时间之间转换的更多相关文章
- MySQL时间戳和时间格式转换函数
MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...
- JS时间戳与时间字符串之间的相互转换
时间字符串 转 时间戳 /** * 时间字符串 转 时间戳 * @param {String} time_str 时间字符串(格式"2014-07-10 10:21:12") * ...
- [linux]date命令时间戳和时间之间的转换
非常多时候我们查看数据库的数据,或者是一些别人系统中的数据须要用时间戳来查询.或者查询出来的结果是个时间戳. 还有时候,查询条件须要输入时间戳. 我之前的办法就是用在线工具来完毕,后来用mac了.我觉 ...
- date命令时间戳和时间之间的转换
这里是在mac下的操作,主要就是用date这个命令,更多的用法用man命令查看 字符串格式时间 TO 时间戳我们知道date 命令可以直接把当前时间转化为时间戳 # date +%s143678152 ...
- 使用java代码将时间戳和时间互相转换
时间戳转时间: SimpleDateFormat simpleDateFormat = null; simpleDateFormat = new SimpleDateFormat("yyyy ...
- php 时间戳和时间的转换
PHP的时间戳与具体时间转化 三个内置函数: time() //获取UNIX系统时间戳 mktime(hour,minute,second,month,day,year) //将指定时间转化为时间戳 ...
- js时间戳和时间格式之间的转换
//时间戳转换成日期时间2014-8-8 下午11:40:20 function formatDate(ns){ return new Date(parseInt(ns) * 1000).toLoca ...
- JS UNIX 时间戳与时间格式转换
上代码,不多说了,这个没啥说的,记录一下: var date = new Date() // Date 2019-03-05T13:50:39.775Z // 获取1970 至今的毫秒数 var ti ...
- js时间戳转化时间格式
// 判断是否前面补0 add0 (m) { return m < 10 ? '0' + m : m }, // 时间转化 timeFormat (timestamp) { // timesta ...
随机推荐
- 全排列问题Ⅰ(Java实现)
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...
- Codeforces 374C - Inna and Dima
374C - Inna and Dima 思路:dfs+记忆化搜索 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- java高并发解决方案
高并发的解决方法有两种: 1.使用缓存 2.使用生成静态页面: (代码质量,不要性能低下的sql和代码.有的一条sql搞定的事,有人用了多个循环才能搞定.取决于程序员的经验!(还有就是从最基础的地方优 ...
- mui 访问系统相册将图片显示到网页
访问系统相返回值为一个对象,通过转换为字符串可以查看,path.files[0]为返回路径去除路径赋值到src 调用摄像头返回的相片的path为一个路径通过 plus.io.resolveLocalF ...
- canvas功能函数
封装一下常用的函数, 输入:通过一些固定的值,得到一个圆形,一个心形,一个波浪,一个涟漪,一个抛物线,一个自由弹起的过程. 返回:x,y坐标. 注意: (1)坐标轴的位置,有的在0,0有的可能不在. ...
- Feign二: @FeignClient 接口调用
在项目的启动文件加入:@EnableFeignClients 注解, import org.springframework.boot.SpringApplication; import org.spr ...
- LeetCode--255--用队列实现栈(java版)
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- 机器学习ML策略
1.为什么是ML策略 例如:识别cat分类器的识别率是90%,怎么进一步提高识别率呢? 想法: (1)收集更多数据 (2)收集更多的多样性训练样本 (3)使用梯度下降训练更长时间 (4)尝试Adam代 ...
- Symmetric Tree leetcode java
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- Remove Element leetcode java
问题描述: Given an array and a value, remove all instances of that value in place and return the new len ...