js中怎么获取当前年份、月份和日期

JavaScript017

js中怎么获取当前年份、月份和日期,第1张

JS有自带的日期函数,获取如下:var date = new Date()var year = date.getFullYear()//获取年份var month = date.getMonth()+1//获取月份,因为是从0-11所以要+1var day = date.getDate()//获取天数var hour = date.getHours()//获取小时var minute = date.getMinutes()//获取分钟var second = date.getSeconds()//获取秒

<script type="text/javascript">

var oldDate = new Date(2011,4,23,0,0,0)

var nowDate = new Date()

var week = "日,一,二,三,四,五,六".split(',')

var str = "今天是"+nowDate.getFullYear()+"年"+(nowDate.getMonth() + 1)+"月"+nowDate.getDate()+"日 星期"+week[nowDate.getDay()]+" 我们已经认识"+parseInt((nowDate.getTime() - oldDate.getTime())/ 86400000)+"天了"

document.write(str)

</script>

javascript 自带有个对象(构造函数),Date().下面是代码:

var myDate = new Date()//实例一个时间对象;

myDate.getFullYear() //获取系统的年;

myDate.getMonth()+1 //获取系统月份,由于月份是从0开始计算,所以要加1

myDate.getDate()// 获取系统日,

myDate.getHours()//获取系统时,

myDate.getMinutes()//分

myDate.getSeconds()//秒