自定义函数 IsDateTime() IsNumeric() PA_CDate()

来源(www.asm32.net)

自定义函数 IsDateTime() IsNumeric() PA_CDate()
4-03-01.aspx
<!-- 程序名称: 4-03-01.aspx -->
<!-- 程序功能: aspx页面中添加一个Label标签 -->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="4-03-01.aspx.cs" Inherits="_4_03_01" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>自定义函数测试</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label id="Label1" runat="server" Height="52px" Text="label1" Width="630px">
        </asp:Label></div>
    </form>
</body>
</html>


4-03-01.aspx.cs
// 程序名称: 4-03-01.aspx.cs
// 程序功能: 4-03-01.aspx的代码文件

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _4_03_01 : System.Web.UI.Page {

    // IsNumeric Function
    static bool IsNumeric(object Expression) {
    // Variable to collect the Return value of the TryParse method.
        bool isNum;

    // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
        double retNum;

    // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
    // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
        isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
        return isNum;
    }

    // IsDateTime Function
    static bool IsDateTime(object Expression) {
        bool isDateTime;
        DateTime retDateTime;

        isDateTime = DateTime.TryParse(Convert.ToString(Expression),
                System.Globalization.DateTimeFormatInfo.InvariantInfo,
                System.Globalization.DateTimeStyles.AssumeUniversal,
                // AdjustToUniversal(本地时间) AssumeUniversal(本地时间+8)
                out retDateTime );
        return isDateTime;
    }

    static DateTime PA_CDate(object Expression) {
        return DateTime.Parse(Expression.ToString(),
                System.Globalization.DateTimeFormatInfo.InvariantInfo,
                System.Globalization.DateTimeStyles.AdjustToUniversal
                // AdjustToUniversal(本地时间) AssumeUniversal(本地时间+8)
        );
    }

    protected void Page_Load(object sender, EventArgs e) {
        string[] A_strDateTime = new string[] {
                "2009-02-11",
                "2009-02-11 08:00:00",
                "13-may-2008 12:00:33",
                "13 may 2008 12:00:33",
                "29-feb-2008 12:00:33"};
        string m_strValue = Request.QueryString["id"];
        string m_strResult = "IsNumeric(\"" + m_strValue + "\")=" + IsNumeric(m_strValue) + "<br>\r\n";
        double m_dblValue = IsNumeric(m_strValue) ? Convert.ToInt32(m_strValue) : 0;
        
        if(!IsNumeric(m_strValue)) m_strValue="0";

        m_strResult += "selet * from pre where preid =" + m_dblValue;
        m_strResult += "<br>\r\n" + m_strValue + "\r\n" + IsNumeric(m_strValue);

        m_strResult += "<br>\r\n" + HttpUtility.HtmlEncode("<<= test & html encode & =>>");

        for(int i=0; i<A_strDateTime.Length; i++){
            m_strResult += "<br>\r\nIsDateTime(A_strDateTime[" + i + "]) ";
            m_strResult += "<br>\r\n=&gt; IsDateTime(\""+A_strDateTime[i]
                    +"\") = " + IsDateTime(A_strDateTime[i]);
            m_strResult += "<br>\r\n=&gt; PA_CDate(\""+A_strDateTime[i]
                    +"\").ToShortDateString() = \"" + PA_CDate(A_strDateTime[i]).ToShortDateString() +"\"";
            m_strResult += "<br>\r\n=&gt; PA_CDate(\""+A_strDateTime[i]
                    +"\").AddYears(1).ToString() = \"" + PA_CDate(A_strDateTime[i]).AddYears(1).ToString() +"\"";
        }

        this.Label1.Text = m_strResult;//.ToString();
    }
}


运行结果
IsNumeric("")=False
selet * from pre where preid =0
0 True
<<= test & html encode & =>>
IsDateTime(A_strDateTime[0]) 
=> IsDateTime("2009-02-11") = True
=> PA_CDate("2009-02-11").ToShortDateString() = "2009-2-11"
=> PA_CDate("2009-02-11").AddYears(1).ToString() = "2010-2-11 0:00:00"
IsDateTime(A_strDateTime[1]) 
=> IsDateTime("2009-02-11 08:00:00") = True
=> PA_CDate("2009-02-11 08:00:00").ToShortDateString() = "2009-2-11"
=> PA_CDate("2009-02-11 08:00:00").AddYears(1).ToString() = "2010-2-11 8:00:00"
IsDateTime(A_strDateTime[2]) 
=> IsDateTime("13-may-2008 12:00:33") = True
=> PA_CDate("13-may-2008 12:00:33").ToShortDateString() = "2008-5-13"
=> PA_CDate("13-may-2008 12:00:33").AddYears(1).ToString() = "2009-5-13 12:00:33"
IsDateTime(A_strDateTime[3]) 
=> IsDateTime("13 may 2008 12:00:33") = True
=> PA_CDate("13 may 2008 12:00:33").ToShortDateString() = "2008-5-13"
=> PA_CDate("13 may 2008 12:00:33").AddYears(1).ToString() = "2009-5-13 12:00:33"
IsDateTime(A_strDateTime[4]) 
=> IsDateTime("29-feb-2008 12:00:33") = True
=> PA_CDate("29-feb-2008 12:00:33").ToShortDateString() = "2008-2-29"
=> PA_CDate("29-feb-2008 12:00:33").AddYears(1).ToString() = "2009-2-28 12:00:33"




Link: http://www.asm32.net/article_details.aspx?id=4416


浏览次数 205 发布时间 2009-02-11 22:35:40 从属分类 asp.net(C#) 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1