时间DateTime类型转换为字符串String格式方法汇总 获得最后一次编译时间

在实际的软件开发工作中,我们通常需要记录某个工程的最后编译时间,原来在C++中,我们有个__DATE__,__TIME__,__FILE__,__LINE__这样的异性宏定义可以使用,但是在C#中,不能使用,但是可以用以下语句来获得最后编译时间。

 

System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location)

 

编程的过程中,通常需要将日期型数据按照一定的格式输出,当然,输出结果肯定是字符串。为此,我们需要使用 System.Date Time 类的 ToString() 方法,并为其指定格式字符串。
  MSDN 中,System.Globalization.Date TimeFormatInfo 类的概述里对模式字符串有非常详细的说明,因此,这里我只对常用的一些格式进行说明,首先请看下表:
d月中的某一天一位数的日期没有前导零
dd月中的某一天一位数的日期有一个前导零
ddd周中某天的缩写名称在 AbbreviatedDayNames 中定义
dddd周中某天的完整名称在 DayNames 中定义
M月份数字一位数的月份没有前导零
MM月份数字一位数的月份有一个前导零
MMM月份的缩写名称在 AbbreviatedMonthNames 中定义
MMMM月份的完整名称在 MonthNames 中定义
y不包含纪元的年份如果不包含纪元的年份小于 10,则显示不具有前导零的年份
yy不包含纪元的年份如果不包含纪元的年份小于 10,则显示具有前导零的年份
yyyy包括纪元的四位数的年份 
h12 小时制的小时一位数的小时数没有前导零
hh12 小时制的小时一位数的小时数有前导零
H24 小时制的小时一位数的小时数没有前导零
HH24 小时制的小时一位数的小时数有前导零
m分钟一位数的分钟数没有前导零
mm分钟一位数的分钟数有一个前导零
s一位数的秒数没有前导零
ss一位数的秒数有一个前导零

  为了便于大家的理解,不妨试试下面的程序:

private void TestDateTimeToString() {    DateTime now = DateTime.Now;    string format;    this.textBox1.Text = "";   
 format = "yyyy-MM-dd HH:mm:ss";    this.textBox1.AppendText(format + ": " + now.ToString(format) + "\n");    
 format = "yy年M日d日";    this.textBox1.AppendText(format + ": " + now.ToString(format) + "\n");}

  这段程序将输出结果:

yyyy-MM-dd HH:mm:ss: 2002-08-26 17:03:04
yy年M日d日: 02年8日26日

DateTime dt = DateTime.Now;
//    Label1.Text = dt.ToString();//2005-11-5 13:21:25
//    Label2.Text = dt.ToFileTime().ToString();//127756416859912816
//    Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
//    Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
//    Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
//    Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
//    Label7.Text = dt.ToOADate().ToString();//38661.5565508218
//    Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
//    Label9.Text = dt.ToShortTimeString().ToString();//13:21
//    Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
//     2005-11-5 13:30:28.4412864
//    Label1.Text = dt.Year.ToString();//2005
//    Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
//    Label3.Text = dt.DayOfWeek.ToString();//Saturday
//    Label4.Text = dt.DayOfYear.ToString();//309
//    Label5.Text = dt.Hour.ToString();//13
//    Label6.Text = dt.Millisecond.ToString();//441
//    Label7.Text = dt.Minute.ToString();//30
//    Label8.Text = dt.Month.ToString();//11
//    Label9.Text = dt.Second.ToString();//28
//    Label10.Text = dt.Ticks.ToString();//632667942284412864
//    Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
//    Label1.Text = dt.ToString();//2005-11-5 13:47:04
//    Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
//    Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
//    Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
//    Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
//    Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
//    Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
//    Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
//    Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
//    Label10.Text = dt.CompareTo(dt).ToString();//0
    Label11.Text = dt.Add(?).ToString();//问号为一个时间段
//    Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
//    Label2.Text = dt.Equals(dt).ToString();//True
//    Label3.Text = dt.GetHashCode().ToString();//1474088234
//    Label4.Text = dt.GetType().ToString();//System.DateTime
//    Label5.Text = dt.GetTypeCode().ToString();//DateTime
   
//    Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
//    Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
//    Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
//    Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
//    Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
//    Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
//    Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
//    Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
//    Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
//    Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
//    Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT

    Label1.Text =   string.Format("{0:d}",dt);//2005-11-5
    Label2.Text =   string.Format("{0:D}",dt);//2005年11月5日
    Label3.Text =   string.Format("{0:f}",dt);//2005年11月5日 14:23
    Label4.Text =   string.Format("{0:F}",dt);//2005年11月5日 14:23:23
    Label5.Text =   string.Format("{0:g}",dt);//2005-11-5 14:23
    Label6.Text =   string.Format("{0:G}",dt);//2005-11-5 14:23:23
    Label7.Text =   string.Format("{0:M}",dt);//11月5日
    Label8.Text =   string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
    Label9.Text =   string.Format("{0:s}",dt);//2005-11-05T14:23:23
    Label10.Text = string.Format("{0:t}",dt);//14:23
    Label11.Text = string.Format("{0:T}",dt);//14:23:23
    Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
    Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
    Label14.Text = string.Format("{0:Y}",dt);//2005年11月
    Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
    Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);

 C# 时间(几个常用时间,程序运行计时,页面运行计时)

1.DateTime
            DateTime now = System.DateTime.Now;
           now.ToString();                                                       //显示: 2006/08/30 17:31:02
            now.ToString("yyyy-MM-dd hh:mm:ss");                //显示: 2006-08-30 05:39:11
           System.DateTime.MaxValue.ToString();                   //显示: 9999/12/31 23:59:59
           System.DateTime.MinValue.ToString();                   //显示: 0001/01/01 0:00:00
         now.ToLongDateString();                                          //显示: 2006年8月30日
         now.ToLongTimeString();                                         //显示: 17:34:23
          now.ToShortTimeString();                                       //显示: 17:34
          now.ToString() + " " + now.Millisecond.ToString();   //显示: 2006/08/30 17:35:19 484

2.程序运行时间:(单位 : 毫秒)
        System.Diagnostics ; //名称空间
              int x = 0;
              int nu = 0;       
            Stopwatch sw = new Stopwatch();
            sw.Start();
             //程序开始
             for (int i = 0; i < 1000000; i++)
            {
                x += i;
            }
             //程序结束
            sw.Stop();
            this.label1.Text += ",sum=" + x.ToString();
            MessageBox.Show(sw.ElapsedMilliseconds.ToString());
3.计算一个页面执行时间:
        在Global.asax.cs文件中增加以下代码:
          protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            Application["StartTime"] = System.DateTime.Now;
        }
        protected void Application_EndRequest(Object sender, EventArgs e)
        {
            System.DateTime startTime = (System.DateTime)Application["StartTime"];
            System.DateTime endTime = System.DateTime.Now;
            System.TimeSpan ts = endTime - startTime;
            Response.Write("页面执行所用时间:" + ts.Milliseconds + " 毫秒");
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值