前端(js/jquery) 日期和时间戳的转换
一、JavaScript中获取当前时间的时间戳
方法一:
var timestamp=Date.parse(new Date()); ====》结果是:1451441086000
注:这种方式精确到秒,毫秒位置上的用0代替了。
方法二:
var timestamp=(new Date()).valueOf(); ====》结果是:1451441232779
注:这两种方法获取从 1970年1月1日午夜开始的毫秒数
方法三:
javascript 中使用 new Date().getTime() 方法,IE8 以上版本可以使用 直接使用Date.now()方法
//IE8以上版本
if (!Date.now) {
Date.now = function() { return new Date().getTime(); };
}
jQuery 获取时间戳 $.now()
var timestamp = $.now();
二、jquery中将具体时间转换成时间戳
1、引入jquery.extend.date.js插件
<script type="text/javascript" src="../js/jquery.extend.date.js"></script>
2、代码原理
(function($) {
$.extend({
myTime: {
/**
* 当前时间戳
* @return <int> unix时间戳(秒)
*/
CurTime: function(){
return Date.parse(new Date())/1000;
},
/**
* 日期 转换为 Unix时间戳
* @param <string> 2014-01-01 20:20:20 日期格式
* @return <int> unix时间戳(秒)
*/
DateToUnix: function(string) {
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1) - 1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime() / 1000;
},
/**
* 时间戳转换日期
* @param <int> unixTime 待时间戳(秒)
* @param <bool> isFull 返回完整时间(Y-m-d 或者 Y-m-d H:i:s)
* @param <int> timeZone 时区
*/
UnixToDate: function(unixTime, isFull, timeZone) {
if (typeof (timeZone) == 'number')
{
unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
}
var time = new Date(unixTime * 1000);
var ymdhis = "";
ymdhis += time.getUTCFullYear() + "-";
ymdhis += (time.getUTCMonth()+1) + "-";
ymdhis += time.getUTCDate();
if (isFull === true)
{
ymdhis += " " + time.getUTCHours() + ":";
ymdhis += time.getUTCMinutes() + ":";
ymdhis += time.getUTCSeconds();
}
return ymdhis;
}
}
});
})(jQuery);
3、应用
$.mytime.DateToUnix('2015-12-30 12:30:15'); //日期转换为时间戳 $.mytime.UnixToDate(1451442143254); //时间戳转换为日期
前端(js/jquery) 日期和时间戳的转换的更多相关文章
- js 时间日期与时间戳之间转换
1 1.将时间(2017-08-10)转换时间戳 2 var startTime = '2017-08-10'; 3 var startdate = new Date(Date.parse(start ...
- Python 日期和时间戳的转换
Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
- MySQL 日期和时间戳互相转换
① 时间戳转换成日期 FROM_UNIXTIME 例如: 数据表中 invest_time 存储的是时间戳,如 1429063399 使用 FROM_UNIXTIME 可以把时间戳转换为日期: sel ...
- MySQL中日期和时间戳互相转换的函数和方法
时间戳转换成日期 复制代码代码如下: FROM_UNIXTIME 例如: 数据表中 invest_time 存储的是时间戳,如 1429063399 使用 FROM_UNIXTIME 可以把时间戳转 ...
- Python3 日期与时间戳互相转换(函数可调用)
一.前言 在开发中,我们经常会遇到时间戳转换日期,或者日期转换为时间戳: 日期格式:2019-08-01 00:00:00 时间戳格式:1564588800 关于时间戳 Unix时间戳(Unix ti ...
- python中时间、日期、时间戳的转换
1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 # 字符类型的时间 tss1 ...
- jq日期与时间戳互相转换
方法1:$.extend({ myTime: { CurTime: function () { return Date.parse(new Date()) / 1000; }, DateToUnix: ...
- 【js】前端 js/jquery 常用代码和实践
1.获取某天后几天的日期 //d为传入的日期 days为d后面的几天function getAfterDate(d,days){ var dd = new Date(d); dd.setDate(dd ...
随机推荐
- struts2错误:The Struts dispatcher cannot be found.
struts2错误:The Struts dispatcher cannot be found. The Struts dispatcher cannot be found. This is usua ...
- Ubuntu配置Android编译环境
1.Ubuntu安装 VMware安装ubuntu之后,安装Vmware tools: 1.安装完ubuntu后,VMware Workstation菜单:虚拟机-->安装 Vmware Too ...
- 在CentOS上为DiscuzX3安装ImageMagick支持。
找了几篇外文的,写的很好,按照参考安装成功! http://www.tecmint.com/install-imagemagick-in-linux/ ImageMagick is an free o ...
- [hadoop转载]tearsort
1TB排序通常用于衡量分布式数据处理框架的数据处理能力.Terasort是Hadoop中的的一个排序作业,在2008年,Hadoop在1TB排序基准评估中赢得第一名,耗时209秒.那么Tera ...
- Oracle 11gR2 create init script
设置oracle用户变量. [oracle@db01 ~]$ vi /etc/oratab # end line: change db01:/oracle/app/product/11.2.0/db_ ...
- zz android 系统 makefile文件(Android.mk)组织结构
Android.mk脚本结构 下面是main.mk文件包含关系,本文档主要说明的就是这些文件里到底做了什么.(这个文件被根目录下的makefile文件包含) 一. main.mk 1.检查版本 ...
- win7IIS错误修改路径最全的
http://blog.csdn.net/testcs_dn/article/details/8726480 http://www.myexception.cn/asp-dotnet/1341569. ...
- 深入理解 Spring 事务原理
本文由码农网 – 吴极心原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 一.事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供 ...
- Centos系统python2.x升级python3.x
1. Centos7初始安装的python版本为2.7.5 2.下载最新的python3.5.2,解压到/usr/local/src 3. 配置.编译.测试.安装(根据软件的README说明) ./c ...
- MYSQL 索引页 结构图
create table t( a ) not ) default null,primary key(a)); mysql> select * from t; +----+------+ | a ...