鍍金池/ 問答/HTML/ 隱式類型轉(zhuǎn)換的問題

隱式類型轉(zhuǎn)換的問題

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>
<script>
var odate= new Date()
odate.setDate(odate.getDate()+5)
document.write(odate)
document.cookie="username=leo"
document.cookie = 'age=32;expires=' + oDate.toGMTString()
document.cookie="age=32;expires="+odate//在document.cookie = '名稱=值;expires=' +時間;中前半部分已經(jīng)是字符串了,應(yīng)該會進(jìn)行隱式類型轉(zhuǎn)換字符串時間的,為什么還要oDate.toGMTString()?
</script>
問題:在cookie中過期時間:document.cookie = '名稱=值;expires=' + 字符串格式的時間; 中的字符串格式的時間為什么要使用 oDate.toGMTString();這個形式呢?在document.cookie = '名稱=值;expires=' +時間;中前半部分('名稱=值;expires=')已經(jīng)是字符串了,應(yīng)該會進(jìn)行隱式類型轉(zhuǎn)換時間的,為什么還要oDate.toGMTString()?

回答
編輯回答
墨小白

var oDate = new Date() 出來的是本地時間(中國東八區(qū))
oDate.toGMTString()已廢棄,嚴(yán)格來說是 oDate.toUTCString()。轉(zhuǎn)化成的是零時區(qū)的UTC時間。

2017年4月12日 17:48
編輯回答
兮顏

這個東西輸出一下就知道了,這并不是轉(zhuǎn)換字符串是轉(zhuǎn)換時區(qū)

var odate= new Date()
console.log(odate.toString())
console.log(odate.toGMTString())
2017年2月26日 06:07