时间戳的三种情况:一java中 new Date.getTime()--13位毫秒计数
二mysql中 Select UNIX_TIMESTAMP(NOW());--10位秒计数
三js中 var timestamp = Date.parse(new Date());--13位毫秒计数后三位为000
var timestamp = (new Date()).valueOf();--13位毫秒计数
var timestamp=new Date().getTime();--13位毫秒计数
Java中:
/* * 将时间转换为时间戳 */ public static String dateToStamp(String s) throws ParseException{ String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse(s); long ts = date.getTime(); res = String.valueOf(ts); return res; }
/* * 将时间戳转换为时间 */ public static String stampToDate(String s){ String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(s); Date date = new Date(lt); res = simpleDateFormat.format(date); return res; }
js中
|
//(2)、将指定日期转换为时间戳。
var t = "2017-12-08 20:5:30"; // 月、日、时、分、秒如果不满两位数可不带0.
var T = new Date(t); // 将指定日期转换为标准日期格式。Fri Dec 08 2017 20:05:30 GMT+0800 (中国标准时间)
1--------console.log(T.getTime()) // 将转换后的标准日期转换为时间戳。
var t = 787986456465; // 当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
2---------console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 (中国标准时间)
var t2 = "2017-5-8 12:50:30";
3---------console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 (中国标准时间)
var t3 = "2017-10-1";
4---------console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 (中国标准时间) 不设定时分秒,则默认转换为00:00:00
var timestamp = '1425553097';
var d = new Date(timestamp * 1000); //根据时间戳生成的时间对象
var date = (d.getFullYear()) + "-" +
(d.getMonth() + 1) + "-" +
(d.getDate()) + " " +
(d.getHours()) + ":" +
(d.getMinutes()) + ":" +
(d.getSeconds());
document.write(date);//在页面写入数据
5---------console.log(date);
mysql中
select FROM_UNIXTIME(1156219870);//时间戳转日期 输出:2006-08-22 12:11:10
Select UNIX_TIMESTAMP('2006-11-04 12:23:00');//转时间戳
SELECT *FROM u_animalheat WHERE DATE_FORMAT(FROM_UNIXTIME(时间戳字段),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') //根据 时间戳 查询当天数据
SELECT * FROM u_animalheat WHERE YEARWEEK(FROM_UNIXTIME(时间戳字段)) = YEARWEEK(NOW())//根据 时间戳 查询当周数据
SELECT * FROM u_animalheat WHERE DATE_FORMAT(FROM_UNIXTIME(时间戳字段),'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')//根据 时间戳 查询当月数据
SELECT * FROM 表名 WHERE DATE_FORMAT(FROM_UNIXTIME(时间戳字段),'%Y') = DATE_FORMAT(NOW(),'%Y') ORDER BY FROM_UNIXTIME(时间戳字段)/根据 时间戳 查询当年数据