C# DateTime 用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# DateTime 用法相关的知识,希望对你有一定的参考价值。

参见https://msdn.microsoft.com/zh-cn/library/system.datetime%28v=vs.110%29.aspx

DateTime.Date:返回实例的日期

DateTime.Day:返回该月中的第几天

DateTime.Now:返回当前时间,精确到秒

DateTime.Today:返回当前时间,精确到天

以DateTime.Now为例,常用的方法有ToBinary()ToString()ToString(String),ToShortDateString()ToShortTimeString()

DateTime.Now.ToBinary(); ----返回当前时间,并转换为64位二进制;

DateTime.Now.ToString();---返回当前时间,并转换为字符串;

DateTime.Now.ToShortDateString();---将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式;

DateTime.Now.ToShortTimeString();---将当前 DateTime 对象的值转换为其等效的短时间字符串表示形式;

DateTime.Now.ToString(String);---以指定的日期格式返回当前时间;

MSDN实例程序:

using System;

public class DateToStringExample
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07);
      // Create an array of standard format strings.
      string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
                               "R", "s", "t", "T", "u", "U", "y"};
      // Output date and time using each standard format string.
      foreach (string standardFmt in standardFmts)
         Console.WriteLine("{0}: {1}", standardFmt, 
                           dateValue.ToString(standardFmt));
      Console.WriteLine();

      // Create an array of some custom format strings.
      string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
                             "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
      // Output date and time using each custom format string.
      foreach (string customFmt in customFmts)
         Console.WriteLine("‘{0}‘: {1}", customFmt,
                           dateValue.ToString(customFmt));
   }
}
// This example displays the following output to the console:
//       d: 6/15/2008
//       D: Sunday, June 15, 2008
//       f: Sunday, June 15, 2008 9:15 PM
//       F: Sunday, June 15, 2008 9:15:07 PM
//       g: 6/15/2008 9:15 PM
//       G: 6/15/2008 9:15:07 PM
//       m: June 15
//       o: 2008-06-15T21:15:07.0000000
//       R: Sun, 15 Jun 2008 21:15:07 GMT
//       s: 2008-06-15T21:15:07
//       t: 9:15 PM
//       T: 9:15:07 PM
//       u: 2008-06-15 21:15:07Z
//       U: Monday, June 16, 2008 4:15:07 AM
//       y: June, 2008
//       
//       ‘h:mm:ss.ff t‘: 9:15:07.00 P
//       ‘d MMM yyyy‘: 15 Jun 2008
//       ‘HH:mm:ss.f‘: 21:15:07.0
//       ‘dd MMM HH:mm:ss‘: 15 Jun 21:15:07
//       ‘\Mon\t\h\: M‘: Month: 6
//       ‘HH:mm:ss.ffffzzz‘: 21:15:07.0000-07:00



在实际使用时:
          string TIME = DateTime.Now.ToString("yyyy-MM-dd");//将日期以2016-03-08的形式输出;
           string sqlstr1="delete from T_Origin where datetime<"+"‘"+TIME+"‘";
           string sqlstr2 = "delete from T_Result where datetime<" + "‘" + TIME + "‘";
           Debug.WriteLine(sqlstr1);
程序的输出结果为:
delete fron T_Origin where datetime<2016-03-08;

以上是关于C# DateTime 用法的主要内容,如果未能解决你的问题,请参考以下文章

转 C#关于DateTime得到的当前时间的格式和用法

c# ?和?? 用法

C#DateTime的用法?

java调用c#的webservice,如何传递DateTime

C# DateTime 与时间戳相互转换

C#第3章学习要点--常用类和结构的用法