js 时间戳(毫秒级) 和 日期格式 (2011-12-13 01:45:30) 互转
2014-10-28 20:48
281 查看
// 复制粘贴 回车测试
//编码 utf-8
//在网上找了N旧 ,就没一个对的,那些 get 时分秒的 除了getUTCXXX,其它 都不能用,我是菜鸟,我也不知道为什么,整了好久 才改成 这种显示模式的
//时间戳 转 日期
//返回 格式 2011-11-12 05:34:12
function formatDate(unix) {
var now = new Date(parseInt(unix) * 1);
now = now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
if(now.indexOf("下午") > 0) {
var temp1 = now.substring(0,now.indexOf("下午")); //2014/7/6
var temp2 = now.substring(now.indexOf("下午")+2,now.length); // 5:17:43
var temp3 = temp2.substring(0,1); // 5
var temp4 = parseInt(temp3); // 5
temp4 = 12 + temp4; // 17
var temp5 = temp4 + temp2.substring(1,temp2.length); // 17:17:43
now = temp1 + temp5; // 2014/7/6 17:17:43
now = now.replace("/","-"); // 2014-7/6 17:17:43
now = now.replace("/","-"); // 2014-7-6 17:17:43
}else {
var temp1 = now.substring(0,now.indexOf("上午")); //2014/7/6
var temp2 = now.substring(now.indexOf("上午")+2,now.length); // 5:17:43
var temp3 = temp2.substring(0,1); // 5
var index = 1;
var temp4 = parseInt(temp3); // 5
if(temp4 == 0 ) { // 00
temp4 = "0"+temp4;
}else if(temp4 == 1) { // 10 11 12
index = 2;
var tempIndex = temp2.substring(1,2);
if(tempIndex != ":") {
temp4 = temp4 + "" + tempIndex;
}else { // 01
temp4 = "0"+temp4;
}
}else { // 02 03 ... 09
temp4 = "0"+temp4;
}
var temp5 = temp4 + temp2.substring(index,temp2.length); // 07:17:43
now = temp1 + temp5; // 2014/7/6 07:17:43
now = now.replace("/","-"); // 2014-7/6 07:17:43
now = now.replace("/","-"); // 2014-7-6 07:17:43
}
return now;
}
//日期 转 时间戳
function dateConvertTime(datetime) {
var tmp_datetime = datetime.replace(/:/g,'-');
tmp_datetime = tmp_datetime.replace(/ /g,'-');
var arr = tmp_datetime.split("-");
var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
return parseInt(now.getTime());
}
alert(formatDate("1414398486000"));
alert(dateConvertTime("2014-10-27 16:28:06"));
//编码 utf-8
//在网上找了N旧 ,就没一个对的,那些 get 时分秒的 除了getUTCXXX,其它 都不能用,我是菜鸟,我也不知道为什么,整了好久 才改成 这种显示模式的
//时间戳 转 日期
//返回 格式 2011-11-12 05:34:12
function formatDate(unix) {
var now = new Date(parseInt(unix) * 1);
now = now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
if(now.indexOf("下午") > 0) {
var temp1 = now.substring(0,now.indexOf("下午")); //2014/7/6
var temp2 = now.substring(now.indexOf("下午")+2,now.length); // 5:17:43
var temp3 = temp2.substring(0,1); // 5
var temp4 = parseInt(temp3); // 5
temp4 = 12 + temp4; // 17
var temp5 = temp4 + temp2.substring(1,temp2.length); // 17:17:43
now = temp1 + temp5; // 2014/7/6 17:17:43
now = now.replace("/","-"); // 2014-7/6 17:17:43
now = now.replace("/","-"); // 2014-7-6 17:17:43
}else {
var temp1 = now.substring(0,now.indexOf("上午")); //2014/7/6
var temp2 = now.substring(now.indexOf("上午")+2,now.length); // 5:17:43
var temp3 = temp2.substring(0,1); // 5
var index = 1;
var temp4 = parseInt(temp3); // 5
if(temp4 == 0 ) { // 00
temp4 = "0"+temp4;
}else if(temp4 == 1) { // 10 11 12
index = 2;
var tempIndex = temp2.substring(1,2);
if(tempIndex != ":") {
temp4 = temp4 + "" + tempIndex;
}else { // 01
temp4 = "0"+temp4;
}
}else { // 02 03 ... 09
temp4 = "0"+temp4;
}
var temp5 = temp4 + temp2.substring(index,temp2.length); // 07:17:43
now = temp1 + temp5; // 2014/7/6 07:17:43
now = now.replace("/","-"); // 2014-7/6 07:17:43
now = now.replace("/","-"); // 2014-7-6 07:17:43
}
return now;
}
//日期 转 时间戳
function dateConvertTime(datetime) {
var tmp_datetime = datetime.replace(/:/g,'-');
tmp_datetime = tmp_datetime.replace(/ /g,'-');
var arr = tmp_datetime.split("-");
var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
return parseInt(now.getTime());
}
alert(formatDate("1414398486000"));
alert(dateConvertTime("2014-10-27 16:28:06"));
相关文章推荐
- 在JS中如何把毫秒转换成规定的日期时间格式实例
- 用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
- js中Date日期格式转换以及时间和毫秒之间的转换
- JS、Jquery实现---把后台传过来的时间毫秒数转换成想要的日期格式
- js将毫秒转换成指定格式的日期时间
- js时间戳(毫秒数)转换为日期格式
- 用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
- JS日期格式字符串转换为日期毫秒数和时间毫秒数转换为日期
- 用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
- JS、Jquery实现---把后台传过来的时间毫秒数转换成想要的日期格式
- 用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
- js 时间计算(加减天数、返回当前时间后一个星期的日期,返回格式yyyy-MM-dd)
- js验证日期时间格式正则表达式
- js时间戳转为日期格式
- js时间戳格式化成日期格式函数
- js时间戳怎么转成日期格式
- js时间戳怎么转成日期格式
- js中当前时间的获得以及日期格式的转换
- 把时间戳转换为日期格式的js代码
- js 验证日期时间格式