var date = new Date(str.replace(/[^\d]+/g,','))
console.log(date)
代码演示:<script language="javaScript">
now = new Date(),hour = now.getHours()
if(hour <6){document.write("凌晨好!")}
else if (hour <9){document.write("早上好!")}
else if (hour <12){document.write("上午好!")}
else if (hour <14){document.write("中午好!")}
else if (hour <17){document.write("下午好!")}
else if (hour <19){document.write("傍晚好!")}
else if (hour <22){document.write("晚上好!")}
else {document.write("夜里好!")}
</script>
就是24小时制,然后根据每个时段进行判断显示的。
例如早上9点前为 早上好,12:00前为上午好!
var reg = /GMT\s*\+\d{4}[\s\(\w\)]+/var str = "Fri Feb 14 2014 19:14:32 GMT+0800 (HKT)"
var _date = new Date(Date.parse(str.replace(reg, "")))
var _month = String(_date.getMonth()+1).length > 1 ? (_date.getMonth()+1) : "0"+(_date.getMonth()+1)
var _day = String(_date.getDate()).length > 1 ? (_date.getDate()) : "0"+(_date.getDate())
var _hour = String(_date.getHours()).length > 1 ? (_date.getHours()) : "0"+(_date.getHours())
var _minute = String(_date.getMinutes()).length > 1 ? (_date.getMinutes()) : "0"+(_date.getMinutes())
var _seconds = String(_date.getSeconds()).length > 1 ? (_date.getSeconds()) : "0"+(_date.getSeconds())
var _mm = parseInt(_hour) > 12 ? "下午":"上午"
var result = _date.getFullYear()+"-"+_month +"-"+_day +" "+_mm+" "+_hour+":"+_minute+":"+_seconds