标签:blog element htm set 函数 head body ret 转换
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>菜鸟教程(runoob.com)</title>
6 </head>
7 <body>
8
9 <h2>将字符串转换为 Date 对象。</h2>
10
11 <p id="demo"></p>
12
13 <script>
14
15 var text = ‘{ "name":"Runoob", "initDate":"2013-12-14", "site":"www.runoob.com"}‘;
16 var obj = JSON.parse(text);
17 obj.initDate = new Date(obj.initDate);
18
19 document.getElementById("demo").innerHTML = obj.name + "创建日期: " + obj.initDate;
20
21 </script>
22
23 </body>
24 </html>
结果为:
Runoob创建日期: Sat Dec 14 2013 08:00:00 GMT+0800 (中国标准时间)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h2>字符串转换为 Date 对象</h2>
<p id="demo"></p>
<script>
var text = ‘{ "name":"Runoob", "initDate":"2013-12-14", "site":"www.runoob.com"}‘;
var obj = JSON.parse(text, function (key, value) {
if (key == "initDate") {
return new Date(value);
} else {
return value;
}});
document.getElementById("demo").innerHTML = obj.name + "创建日期:" + obj.initDate;
</script>
</body>
</html>
结果一样.
标签:blog element htm set 函数 head body ret 转换
原文地址:http://www.cnblogs.com/514929hgy/p/7335318.html