function timestamp(time){
var date = new Date(time); //传个时间戳过去就能够了
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';php
return Y+M+D; //2014-04-23 18:55:49:123
}函数
1. js将时间戳转换成日期格式
// 简单的一句代码
var date = new Date(时间戳); //获取一个时间对象 注意:若是是uinx时间戳记得乘于1000。好比php函数time()得到的时间戳就要乘于1000
/**
1. 下面是获取时间日期的方法,须要什么样的格式本身拼接起来就行了
*/
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)
例子
// 好比须要这样的格式 yyyy-MM-dd hh:mm:ss
var date = new Date(1398250549490);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
console.log(Y+M+D+h+m+s); //呀麻碟
// 输出结果:2014-04-23 18:55:49
2. 将日期格式转换成时间戳
// 也很简单
var strtime = '2014-04-23 18:55:49:123';
var date = new Date(strtime); //传入一个时间格式,若是不传入就是获取如今的时间了,这样作不兼容火狐。
// 能够这样作
var arr = strtime.replace(/ |:/g, '-').split('-');
date = new Date(Date.UTC(arr[1], arr[2], arr[3], arr[4], arr[5]));
// 有三种方式获取,在后面会讲到三种方式的区别
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);
/*
三种获取的区别:
第1、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
好比上面代码输出的结果(一眼就能看出区别):
1398250549123
1398250549123
1398250549000
*/
3. 注意
获取到的时间戳除于1000就能够得到unix的时间戳了,在传值给PHP时用获得 ui