Microsoft AJAX Library Cheat Sheet (二)Boolean和Date类型的扩展

本文介绍了如何通过扩展Boolean和Date类型来增强JavaScript的功能,包括静态方法Boolean.parse用于解析布尔值,以及Date类型的多种格式化方法如format和localeFormat等。

 Boolean和Date类型的扩展

(一)使用方式

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
        function a()
        {
            var a = ['a','b','c','d'];
            Array.add(a, 'e');
            alert(a);
        }
    </script> 
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"  />
        <input id="Button1" type="button" value="button" onclick="a();" />
    </form>
</body>
</html>

关键部分ScriptManager 不能少,少了就用不了了.

(二)Boolean和Date类型的扩展

注:标注有[S]的为静态方法,无须实例化对象即可使用。

[S] Boolean.parse (value )

从字符串表示的布尔值解析成相应的Boolean类型。value参数必须为“true”或“false”二者之一,不区分大小写,且可以包含空格。

var b = Boolean.parse("true");

 

Date.format (format)

格式化字符串(区域设定无关)。

var d = new Date();
Sys.Debug.trace (d.format("dddd, dd MMMM yyyy HH:mm:ss"));

 

Date.localeFormat (format)

格式化字符串(区域设定相关)。使用Sys.CultureInfo.CurrentCulture得到区域属性。

var d = new Date();
Sys.Debug.trace (d.localeFormat("dddd, dd MMMM yyyy HH:mm:ss"));

 

[S] Date.parseLocale (value, formats)

从字符串表示的本地时间日期解析成相应的Date类型。使用Sys.CultureInfo.CurrentCulture得到区域属性。

Sys.Debug.trace (Date.parseLocale ("4/10/2001", "yyyy-MM-dd",
"MM/dd/yyyy"));
// Date.parseLocale will skip the first format here as invalid and use
// the second one. If it does not find an appropriate format,
// the function throws an exception.

 

[S] Date.parseInvariant (value, formats)

从字符串表示的区域无关时间日期解析成相应的Date类型。

Sys.Debug.trace (Date.parseInvariant ("4/10/2001", "yyyy-MM-dd",
"MM/dd/yyyy"));
// Date.parseInvariant will skip the first format here as invalid and use
// the second one. If it does not find an appropriate format,
// the function throws an exception.

支持的格式

  1. d: 缩写日期(e.g.: 02/17/2007):
  2. D: 完整日期(e.g: Saturday, 17 February 2007)
  3. t: 缩写时间(e.g.: 22:10)
  4. T: 完整时间(e.g.: 22:10:30)
  5. F: 完整时间日期(e.g.: Saturday, 17 February 2007 22:10:30)
  6. m (or M): 月和日(e.g.: February 17)
  7. s: 可排序的时间日期(e.g.: 2007-02-17T22:10:30)
  8. y (or Y): 年和月(e.g.: 2007 February)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值