码迷,mamicode.com
首页 > Windows程序 > 详细

C# DateTime 用法

时间:2016-03-09 12:34:16      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

参见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 用法

标签:

原文地址:http://www.cnblogs.com/kongqidonglixue/p/5257595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!