【Date类型】JavaScript中的原生对象以及Microsoft AJAX Library中的相关扩展

来源:互联网 发布:java重载返回值 区分 编辑:程序博客网 时间:2024/06/08 15:14

Date原生类型(Date原生类型的扩展见下方)


•UTC时间:国际标准时间
 – GMT, Greenwich Mean Time
• 表示日期和时间
 –一个数字,表示相对于UTC时间1970年1月1日0时整的毫秒数偏移量
–每个Date对象存储的永远只是一个数字
 –时间只有一个,时差是表现上的概念
• Date.prototype.getTimezoneOffset()
 –获得时差的分钟数

 

构造Date对象


• var d1 = new Date();
 –表示当前时间的对象(时差是表现上的概念)
 -如:var d = new Date(2011, 8, 25); 表示2011年9月25号,在这里的12个月份中,分别是0~11个数字

• var d2 = new Date(0);
 –表示UTC时间1970年1月1日0时整
• var d3 = new Date(2007, 5, 6);
 –本地时间2007年6月6日0时整
 –月从0开始(0到11表示一月到十二月)
• var d = new Date(Date.UTC(2007, 5, 6));
 –表示UTC时间2007年6月6日0时整

 

Date对象 → 字符串

• Date.prototype. …
• 与操作系统设置无关,与脚本引擎有关
 – toString(),toDateString(),toTimeString(),
toUTCString()
• 与操作系统设置相关
 – toLocaleString(),toLocaleDateString(),toLocaleTimeString()

 

字符串 → Date对象


• Date.parse(str)
 –返回表示Date的那个数字
 –只可识别特定的格式
• 与特定脚本引擎相关(以下三者相同)
 –t
 – Date.parse(new Date(t).toString());
 – Date.parse(new Date(t).toUTCString());
• 使用价值很低

 

修改和获取Date对象属性


• getTime / setTime
• getFullYear / setFullYear / getUTCFullYear / setUTCFullYear
• getMonth / setMonth / getUTCMonth / setUTCMonth
• getDate / setDate / getUTCDate / setUTCDate
• getDay / setDay / getUTCDay / setUTCDay
• getHours / setHours / getUTCHours / setUTCHours
• getMinutes / setMinutes / getUTCMinutes / setUTCMinutes
• getSeconds / setSeconds / getUTCSeconds / setUTCSeconds
• getMilliseconds / setMilliseconds / getUTCMilliseconds / setUTCMilliseconds

 

Microsoft AJAX Library中对Date原生类型的扩展

注:要使用扩展需添加ScriptManager控件 如:<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

 

•Date对象和字符串之间的转换

• 字符串 → 数值
– Date.parseLocale(value, formats)
– Date.parseInvariant(value, formats)

• 数值 → 字符串
– Date.prototype.format(format)
– Date.prototype.localeFormat(format)

 

• 与Number类型的扩展相似
• 预定义format格式
–i:与JavaScript内置功能相同
–d:短日期格式
–D:长日期格式
–t:短时间格式
–T:长时间格式
–F:完整的时间日期格式
–M, m:月份日期格式
–Y, y:年和月份格式

 

扩展实例:

aspx页面

<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" /><div id="info"></div><script language="javascript" type="text/javascript">function display(text){document.getElementById("info").innerHTML += (text + "<br />");}var now = new Date();display("now.localeFormat('d') = " + now.localeFormat('d'));display("now.localeFormat('D') = " + now.localeFormat('D'));display("now.localeFormat('t') = " + now.localeFormat('t'));display("now.localeFormat('T') = " + now.localeFormat('T'));display("now.localeFormat('F') = " + now.localeFormat('F'));display("now.localeFormat('m') = " + now.localeFormat('m'));display("now.localeFormat('y') = " + now.localeFormat('y'));display("now.localeFormat('yyyy-MM月dd日 hh:mm:ss tt dddd') = " + now.localeFormat('yyyy-MM月dd日 hh:mm:ss tt dddd'));var d = Date.parseLocale("07/7/3", "yy/M/d");display(d.format("i"));</script>


 

原创粉丝点击