获取年、月、日和将时间戳转换成日期格式
// 简单的一句代码var date = new date(时间戳); //获取一个时间对象/** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 2. 更多好用的方法可以在这查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp */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:ssvar 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
以上就是怎么用js获取年月日的详细内容。