js时间转换相关
1.json时间格式转换
function ChangeDateFormat(jsondate) {
if (!jsondate||jsondate.length < 1) {return ""; }
jsondate = jsondate.replace("/Date(", "").replace(")/", "");
if (jsondate.indexOf("+") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("+"));
}
else if (jsondate.indexOf("-") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("-"));
}
var date = new Date(parseInt(jsondate, 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate;
}
//带日期
function ChangeDateTimeFormat(jsondate) {
if (!jsondate || jsondate.length < 1) { return ""; }
jsondate = jsondate.replace("/Date(", "").replace(")/", "");
if (jsondate.indexOf("+") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("+"));
}
else if (jsondate.indexOf("-") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("-"));
}
var date = new Date(parseInt(jsondate));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
}
2.js中两个时间的差
function DateS(a,b,c) {
var arr = ChangeDateFormat(a).split("-");
var starttime = new Date(arr[0], arr[1] - 1, arr[2]);
var starttimes = starttime.getTime();
var arr = ChangeDateFormat(b).split("-");
var lktimes = new Date(arr[0], arr[1] - 1, arr[2]);
var lktimes = starttime.getTime();
var ltime = 0;
if (c === 'y' || c === 'Y') {
ltime = (starttimes - lktimes) / (86400000*365);
}
if (c === 'm' || c === 'M') {
ltime = (starttimes - lktimes) / (86400000 * 365/12);
}
if (c === 'd' || c === 'D') {
ltime = (starttimes - lktimes) / 86400000;
}
if (c === 'h' || c === 'H') {
ltime = (starttimes - lktimes) / 3600000;
}
if (c === 'MM' || c === 'mm') {
ltime = (starttimes - lktimes) / 60000;
}
if (c === 'ss' || c === 'SS') {
ltime = (starttimes - lktimes) / 1000;
}
return ltime;
}
//输入的值与当前时间差
function DateSNow(a, c) {
var arr = ChangeDateFormat(a).split("-");
var starttime = new Date(arr[0], arr[1] - 1, arr[2]);
var starttimes = starttime.getTime();
var myDate = new Date();
var mmDate = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
var lktimes = mmDate.getTime();
var ltime = 0;
if (c === 'y' || c === 'Y') {
ltime = (starttimes - lktimes) / (86400000 * 365);
}
if (c === 'm' || c === 'M') {
ltime = (starttimes - lktimes) / (86400000 * 365 / 12);
}
if (c === 'd' || c === 'D') {
ltime = (starttimes - lktimes) / 86400000;
}
if (c === 'h' || c === 'H') {
ltime = (starttimes - lktimes) / 3600000;
}
if (c === 'MM' || c === 'mm') {
ltime = (starttimes - lktimes) / 60000;
}
if (c === 'ss' || c === 'SS') {
ltime = (starttimes - lktimes) / 1000;
}
return ltime;
}
js时间转换相关的更多相关文章
- JS 时间转换函数 字符串时间转换毫秒(互转)
字符串转化为日期 let util = function(){ Date.prototype.Format = function(fmt) { var o = { "M+" : t ...
- python 时间转换相关
最近需要操作时间的地方相当的多,包括打点,包括时间转换. 罗列最近遇到的两个需求. 1. 关于上篇文章写的base64上传图片的问题,我使用了打点来计算解码需要多少时间.这个对时间精度要求是比较高的. ...
- JS时间转换的一个坑位
在做项目的时候,无意发现了一个小东西. new Date('2018-05-15') new Date('2018-5-15') 输出的结果是不同的,相差了8小时.然后让我回忆到之前看的一个时间转换函 ...
- js 时间转换
//时间转换成时间戳 function DateToUnix(string) { var f = string.split(' ', 2); var d = (f[0] ? f[0] : '').sp ...
- JS时间转换,url编码,jquery返回类型等问题
1.当时间被转换为json格式后会被转换成 /Date(...)/ 这种格式,其中...为时间转换成妙后的一串整数 function changeDateFormat(cellval) { )); v ...
- js时间转换
1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 ...
- js时间转换,能够把时间转换成yyyymmdd格式或yyyymm格式
//type为1则转换成yyyymmdd格式,type为2则转换成yyyymm格式 function formatTime(time,type){ var temp_time=new Number(t ...
- js 时间转换毫秒的四种方法(转)
将时间转换为毫秒数的方法有四个: Date.parse()Date.UTCvalueOf()getTime() 1. Date.parse():该方法接受一个表示日期的字符串参数,然后尝试根据这个日期 ...
- PostgreSQL 时间转换
背景:最近频繁使用到时间转换相关的操作,特此小记. 1.实时取最近24小时内数据. select now() - interval '24h'; 通过sql获得符合要求的时间段,当做where条件即可 ...
随机推荐
- 【解题报告】[动态规划] RQNOJ PID5 / 能量项链
原题地址:http://www.rqnoj.cn/problem/5 解题思路: 今天刚刚才知道了区间DP..Orz..本来以为是状态压缩DP,后来看到数据量才发现原来不是.后来参考了别人的题解.自己 ...
- Heritrix源码分析(十二) Heritrix的控制中心(大脑)CrawlController(一)(转)
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/650694 本博客已迁移到本人独立博客: http://www.yun5u.com/ ...
- T-SQL备忘(2):聚合函数运算和NULL
我们看表的数据: 而select AVG(Age) from Member1的结果为27.自己算一下就知道136/6 =22.666.而不是27,因此知道实际上Age为NULL的行没有参与运算.即: ...
- 【转】iOS开发-Protocol协议及委托代理(Delegate)传值
原文网址:http://www.cnblogs.com/GarveyCalvin/p/4210828.html 前言:因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来 ...
- JSTL(fn函数)
JSTL(fn函数) 首先,我们要在页面的最上方引用: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/js ...
- Golang 绘图基础 -绘制简单图形
前一节讲的是 绘图到不同输出源,请看地址: http://www.cnblogs.com/ghj1976/p/3440856.html 上一节的例子效果是通过设置每一个点的的RGBA属性来实现的,这是 ...
- Drupal 7.23:函数drupal_alter()注释
/** * Passes alterable variables to specific hook_TYPE_alter() implementations. * * This dispatch fu ...
- javascript面向对象实例
非私有属性 function Student(name, gender, age, grade, teacher){ this.name = name; this.gender = gender; t ...
- 全方位掌握 NSIS 的操作
NSIS 确实是一个不错的安装程序制作软件.新版本2.0a7真正实现了中文支持和支持 WinXP 的安装对话框.不过要用它实现漂亮的安装界面和完美的安装功能就必须好好的写脚本.而 NSIS 的脚本指令 ...
- bzoj 1492 [NOI2007]货币兑换Cash(斜率dp+cdq分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1492 [题意] 有AB两种货币,每天可以可以付IPi元,买到A券和B券,且A:B= ...