ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换

using System;
using System.Globalization;


public class SamplesDTFI  {

   public static void Main()  {

      // Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
      DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;

      // Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
      // The Gregorian calendar is the default calendar for the en-US culture.
      DateTime myDT = new DateTime( 2002, 1, 3 );

      // Displays the format pattern associated with each format character.
      Console.WriteLine( "FORMAT  en-US EXAMPLE" );
      Console.WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
      Console.WriteLine( "  d     {0}", myDT.ToString("d", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
      Console.WriteLine( "  D     {0}", myDT.ToString("D", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
      Console.WriteLine( "  f     {0}\n", myDT.ToString("f", myDTFI) );
      Console.WriteLine( "  F     {0}", myDT.ToString("F", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
      Console.WriteLine( "  g     {0}\n", myDT.ToString("g", myDTFI) );
      Console.WriteLine( "  G     {0}\n", myDT.ToString("G", myDTFI) );
      Console.WriteLine( "  m     {0}", myDT.ToString("m", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  M     {0}", myDT.ToString("M", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  o     {0}\n", myDT.ToString("o", myDTFI) );
      Console.WriteLine( "  r     {0}", myDT.ToString("r", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  R     {0}", myDT.ToString("R", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  s     {0}", myDT.ToString("s", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
      Console.WriteLine( "  t     {0}", myDT.ToString("t", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
      Console.WriteLine( "  T     {0}", myDT.ToString("T", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
      Console.WriteLine( "  u     {0}", myDT.ToString("u", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
      Console.WriteLine( "  U     {0}\n", myDT.ToString("U", myDTFI) );
      Console.WriteLine( "  y     {0}", myDT.ToString("y", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
      Console.WriteLine( "  Y     {0}", myDT.ToString("Y", myDTFI) );
      Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );

   }

}

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

  d     1/3/2002
        M/d/yyyy (ShortDatePattern)

  D     Thursday, January 03, 2002
        dddd, MMMM dd, yyyy (LongDatePattern)

  f     Thursday, January 03, 2002 12:00 AM

  F     Thursday, January 03, 2002 12:00:00 AM
        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

  g     1/3/2002 12:00 AM

  G     1/3/2002 12:00:00 AM

  m     January 03
        MMMM dd (MonthDayPattern)

  M     January 03
        MMMM dd (MonthDayPattern)

  o     2002-01-03T00:00:00.0000000

  r     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

  t     12:00 AM
        h:mm tt (ShortTimePattern)

  T     12:00:00 AM
        h:mm:ss tt (LongTimePattern)

  u     2002-01-03 00:00:00Z
        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

  U     Thursday, January 03, 2002 8:00:00 AM

  y     January, 2002
        MMMM, yyyy (YearMonthPattern)

  Y     January, 2002
        MMMM, yyyy (YearMonthPattern)

*/

在平时编码中,经常要把日期转换成各种各样的形式输出或保持,今天专门做了个测试,发现DateTime的ToString()方法居然有这么多的表现形式,和大家一起分享.

DateTime time=DateTime.Now;   //2010-5-28  11:22:02.4552691 星期五

 

              time.To("y")                         "2010年5月"

              time.To("yy")                       "10"

              time.To("yyy")                     "2010"

              time.To("yyyy")                    "2010"

              time.To("Y")                        "2010年5月"

              time.To("m")                       "5月28日"

              time.To("mm")                     "22"

              time.To("mmm")                  "22"

              time.To("mmmm")               "22"

              time.To("M")                      "5月28日"

              time.To("MM")                   "05"

              time.To("MMM")                "五月"

              time.To("MMMM")             "五月"

              time.To("d")                         "2010-5-28"

              time.To("dd")                       "28"

              time.To("ddd")                     "五"

              time.To("dddd")                   "星期五"

              time.To("D")                        "2010年5月28日"

              time.To("hh")                        "11"

              time.To("hhh")                      "11"

              time.To("hhhh")                     "11"

              time.To("HH")                      "11"

              time.To("HHH")                    "11"

              time.To("HHHH")                 "11"

              time.To("s")                         "2010-05-28T11:22:02"

              time.To("ss")                       "02"

              time.To("sss")                      "02"

              time.To("ssss")                    "02"

              time.To("f")                         "2010年5月28日 11:22"

              time.To("ff")                        "45"

              time.To("fff")                       "455"

              time.To("ffff")                      "4552"

              time.To("t")                         "11:22"

              time.To("tt")                        "AM"

              time.To("ttt")                       "AM"

              time.To("tttt")                      "AM"

              time.To("T")                        "11:22:02"

              time.To("zz")                       "+08"

              time.To("zzz")                      "+08:00"

              time.To("zzzz")                    "+08:00"

              time.To("r")                         "Fri, 28 May 2010 11:22:02 GMT"

              time.To("R")                        "Fri, 28 May 2010 11:22:02 GMT"

              time.To("u")                         "2010-05-28 11:22:02Z"

              time.To("U")                        "2010年5月28日 3:11:02"

              time.To("o")                         "2010-05-28T11:22:02.4552691+08:00"

              time.To("O")                        "2010-05-28T11:22:02.4552691+08:00"

              time.To("u")                         "2010-05-28 11:22:02Z"

              time.To("U")                        "2010年5月28日 3:11:02"

              time.To("g")                         "2010-5-28 11:22"

              time.To("gg")                       "公元"

              time.To("ggg")                     "公元"

              time.To("gggg")                   "公元"

              time.To("G")                        "2010-5-28 11:22:02"

能够取出单一的某个部分的字符串可以组合起来使用,如:

time.ToString("gg yyyy/MM/dd hh:mm:ss dddd")              "公元 2010-05-28 11:56:26 星期五"

因为此处gg,yyyy,MM,dd,hh,mm,ss,dddd都只是取出单一的部分.

像下面的组合则不行:

time.ToString("gg y")     "公元 10"   //结果不是我们需要的"公元 2010年5月"

因为y代表的是"2010年5月",不是单一的功能. 



在 ASP.net c# /中

  我们可以通过使用DataTime这个类来获取当前的时间。通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04)、时间(12:12:12)、日期+时间(2008-09-04 12:11:10)等。


//获取日期+时间
DateTime.Now.ToString();            // 2008-9-4 20:02:10
DateTime.Now.ToLocalTime().ToString();        // 2008-9-4 20:12:12

//获取日期
DateTime.Now.ToLongDateString().ToString();    // 2008年9月4日
DateTime.Now.ToShortDateString().ToString();    // 2008-9-4
DateTime.Now.ToString("yyyy-MM-dd");        // 2008-09-04
DateTime.Now.Date.ToString();            // 2008-9-4 0:00:00

//获取时间
DateTime.Now.ToLongTimeString().ToString();   // 20:16:16
DateTime.Now.ToShortTimeString().ToString();   // 20:16
DateTime.Now.ToString("hh:mm:ss");        // 08:05:57
DateTime.Now.TimeOfDay.ToString();        // 20:33:50.7187500

//其他
DateTime.ToFileTime().ToString();       // 128650040212500000
DateTime.Now.ToFileTimeUtc().ToString();   // 128650040772968750
DateTime.Now.ToOADate().ToString();       // 39695.8461709606
DateTime.Now.ToUniversalTime().ToString();   // 2008-9-4 12:19:14

DateTime.Now.Year.ToString();          获取年份   // 2008
DateTime.Now.Month.ToString();      获取月份   // 9
DateTime.Now.DayOfWeek.ToString(); 获取星期   // Thursday
DateTime.Now.DayOfYear.ToString(); 获取第几天   // 248
DateTime.Now.Hour.ToString();          获取小时   // 20
DateTime.Now.Minute.ToString();     获取分钟   // 31
DateTime.Now.Second.ToString();     获取秒数   // 45

//n为一个数,可以数整数,也可以事小数
dt.AddYears(n).ToString();   //时间加n年
dt.AddDays(n).ToString();   //加n天
dt.AddHours(n).ToString();   //加n小时
dt.AddMonths(n).ToString();   //加n个月
dt.AddSeconds(n).ToString();   //加n秒
dt.AddMinutes(n).ToString();   //加n分


SQL语句使用时间和日期的函数


getdate():获取系统当前时间
dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate())
datediff(datepart,startdate,enddate):计算两个时间的差值,比如:datediff(yy,getdate(),'2008-08-08')
dataname(datepart,date):获取时间不同部分的值,返回值为字符串
datepart(datepart,date):和datename相似,只是返回值为整型
day(date):获取指定时间的天数
month(date):获取指定时间的月份
year(date):获取指定时间的年份

select year(getdate())  :当前年份

SQL在读取的时候转换时间格式的问题

时间是 DateTime 类型的  现在在一些地方我要只显示年/月/日 把后面的具体几点几分 把现实  这个转换怎么写

<%# Eval("NTime")%>     这种情况下怎么转换

绑在GridView里边的     这种情况下怎么转换

这两个地方我都要转化成 只显示年/月/日  怎么写
<%#Convert.ToDateTime(Eval("NTime")).toString("YYYY/MM/dd")%>      --只要Ntime能转成时间格式即可。
               --这里的 “Ntime”列的类型必须为时间类型,否则转换不了

以上两种,转换过来的格式都为:年/月/日 .
 
<%# Eval("NTime")%>:
<%# Eval(Container.DataItem, "NTime","{0:d}")%> 

你可能感兴趣的:(ASP.NET)