DateTime属性使用

简介: /* Example9_4.cs illustrates the use of DateTime properties and methods */ using System; class Example9_4 { public static void Main(...
/*
  Example9_4.cs illustrates the use of DateTime properties and methods
*/

using System;

class Example9_4
{

  public static void Main()
  {

    // use the Now and UtcNow properties to get the currrent
    // date and time
    Console.WriteLine("DateTime.Now = " + DateTime.Now);
    Console.WriteLine("DateTime.UtcNow = " + DateTime.UtcNow);
    DateTime myDateTime = DateTime.Now;
    Console.WriteLine("myDateTime = " + myDateTime);
    DateTime myDateTime2 = DateTime.UtcNow;
    Console.WriteLine("myDateTime = " + myDateTime);

    // display the Date, Day, DayOfWeek, DayOfYear, Ticks, and
    // TimeOfDayProperties of myDateTime
    Console.WriteLine("myDateTime.Date = " + myDateTime.Date);
    Console.WriteLine("myDateTime.Day = " + myDateTime.Day);
    Console.WriteLine("myDateTime.DayOfWeek = " + myDateTime.DayOfWeek);
    Console.WriteLine("myDateTime.DayOfYear = " + myDateTime.DayOfYear);
    Console.WriteLine("myDateTime.Ticks = " + myDateTime.Ticks);
    Console.WriteLine("myDateTime.TimeOfDay = " + myDateTime.TimeOfDay);

    // use the Compare() method to compare DateTime instances
    DateTime myDateTime3 = new DateTime(2004, 1, 13);
    DateTime myDateTime4 = new DateTime(2004, 1, 14);
    Console.WriteLine("myDateTime3 = " + myDateTime3);
    Console.WriteLine("myDateTime4 = " + myDateTime4);
    int intResult = DateTime.Compare(myDateTime3, myDateTime4);
    Console.WriteLine("DateTime.Compare(myDateTime3, myDateTime4) = " +
      DateTime.Compare(myDateTime, myDateTime2));

    // use the overloaded less than operator (<) to compare two
    // DateTime instances
    bool boolResult = myDateTime3 < myDateTime4;
    Console.WriteLine("myDateTime3 < myDateTime4 is " +
      boolResult);

    // use the Equals() method to compare DateTime instances
    boolResult = DateTime.Equals(myDateTime3, myDateTime4);
    Console.WriteLine("DateTime.Equals(myDateTime3, myDateTime4) = " +
      boolResult);
    boolResult = myDateTime3.Equals(myDateTime4);
    Console.WriteLine("myDateTime3.Equals(myDateTime4) is " +
      boolResult);

    // use the DaysInMonth() method to retrieve the number of days
    // in a particular month and year
    int days = DateTime.DaysInMonth(2004, 1);
    Console.WriteLine("DateTime.DaysInMonth(2004, 1) = " +
      days);

    // use the IsLeapYear() method to determine if a particular
    // year is a leap year
    boolResult = DateTime.IsLeapYear(2004);
    Console.WriteLine("DateTime.IsLeapYear(2004) = " +
      boolResult);

    // use the Parse() method to convert strings to DateTime instances
    DateTime myDateTime5 = DateTime.Parse("1/13/2004");
    DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
    Console.WriteLine("myDateTime5 = " + myDateTime5);
    Console.WriteLine("myDateTime6 = " + myDateTime6);

    // use the Add() method to add a TimeSpan to a DateTime
    TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
    DateTime myDateTime7 = myDateTime6.Add(myTimeSpan);
    Console.WriteLine("myTimeSpan = " + myTimeSpan);
    Console.WriteLine("myDateTime6.Add(myTimeSpan) = " +
      myDateTime7);

    // use the Subtract() method to subtract a TimeSpan from a DateTime
    myDateTime7 = myDateTime6.Subtract(myTimeSpan);
    Console.WriteLine("myDateTime6.Subtract(myTimeSpan) = " +
      myDateTime7);

    // use the overloaded addition operator (+) to add a TimeSpan
    // to a DateTime
    DateTime myDateTime8 = myDateTime6 + myTimeSpan;
    Console.WriteLine("myDateTime6 + myTimeSpan = " + myDateTime8);

    // use the overloaded subtraction operator (-) to subtract
    // a TimeSpan from a DateTime
    myDateTime8 = myDateTime6 - myTimeSpan;
    Console.WriteLine("myDateTime6 - myTimeSpan = " + myDateTime8);

    // use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and
    // AddSeconds() methods to add periods to a DateTime
    DateTime myDateTime9 = new DateTime(2004, 1, 1);
    Console.WriteLine("Initial myDateTime9 = " + myDateTime9);
    myDateTime9 = myDateTime9.AddYears(1);
    myDateTime9 = myDateTime9.AddMonths(5);
    myDateTime9 = myDateTime9.AddDays(3);
    myDateTime9 = myDateTime9.AddMinutes(30);
    myDateTime9 = myDateTime9.AddSeconds(15);
    Console.WriteLine("Final myDateTime9 = " + myDateTime9);

    // use the ToFileTime() method to convert a DateTime to
    // an operating system file timestamp
    DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5);
    long myFileTime = myDateTime10.ToFileTime();
    Console.WriteLine("myDateTime10.ToFileTime() = " +
      myDateTime10.ToFileTime());

    // use the FromFileTime() method to convert
    // an operating system file timestamp to a DateTime
    DateTime myDateTime11 = DateTime.FromFileTime(myFileTime);
    Console.WriteLine("DateTime.FromFileTime() = " +
      myDateTime11);

    // use the ToLongDateString() and ToShortDateString() methods
    // to convert the date parts of a DateTime to long and short
    // date strings
    Console.WriteLine("myDateTime11 = " + myDateTime11);
    Console.WriteLine("myDateTime11.ToLongDateString() = " +
      myDateTime11.ToLongDateString());
    Console.WriteLine("myDateTime11.ToShortDateString() = " +
      myDateTime11.ToShortDateString());

    // use the ToLongTimeString() and ToShortTimeString() methods
    // to convert the time parts of a DateTime to long and short
    // time strings
    Console.WriteLine("myDateTime11.ToLongTimeString() = " +
      myDateTime11.ToLongTimeString());
    Console.WriteLine("myDateTime11.ToShortTimeString() = " +
      myDateTime11.ToShortTimeString());

    // use the ToString() method to convert a DateTime
    // to a string
    DateTime myDateTime12 = new DateTime(2004, 1, 12, 22, 2, 10);
    Console.WriteLine("myDateTime12.ToString() = " +
      myDateTime12.ToString());
    Console.WriteLine("myDateTime12.ToString(\"MMMM dd, yyyy\") = " +
      myDateTime12.ToString("MMMM dd, yyyy"));
    Console.WriteLine("myDateTime12.ToString(\"d\") = " +
      myDateTime12.ToString("d"));
    Console.WriteLine("myDateTime12.ToString(\"D\") = " +
      myDateTime12.ToString("D"));
    Console.WriteLine("myDateTime12.ToString(\"f\") = " +
      myDateTime12.ToString("f"));
    Console.WriteLine("myDateTime12.ToString(\"F\") = " +
      myDateTime12.ToString("F"));
    Console.WriteLine("myDateTime12.ToString(\"g\") = " +
      myDateTime12.ToString("g"));
    Console.WriteLine("myDateTime12.ToString(\"G\") = " +
      myDateTime12.ToString("G"));
    Console.WriteLine("myDateTime12.ToString(\"m\") = " +
      myDateTime12.ToString("m"));
    Console.WriteLine("myDateTime12.ToString(\"r\") = " +
      myDateTime12.ToString("r"));
    Console.WriteLine("myDateTime12.ToString(\"s\") = " +
      myDateTime12.ToString("s"));
    Console.WriteLine("myDateTime12.ToString(\"t\") = " +
      myDateTime12.ToString("t"));
    Console.WriteLine("myDateTime12.ToString(\"T\") = " +
      myDateTime12.ToString("T"));
    Console.WriteLine("myDateTime12.ToString(\"u\") = " +
      myDateTime12.ToString("u"));
    Console.WriteLine("myDateTime12.ToString(\"U\") = " +
      myDateTime12.ToString("U"));
    Console.WriteLine("myDateTime12.ToString(\"y\") = " +
      myDateTime12.ToString("y"));

  }

}
相关文章
|
6月前
|
C#
53.c#:datetime类
53.c#:datetime类
146 1
|
6月前
date标签
date标签。
27 2
|
6月前
|
JavaScript 前端开发
date对象是什么?有什么用
date对象是什么?有什么用
94 0
Easyui validatebox增加对time、date、datetime的验证,时间格式化
Easyui validatebox增加对time、date、datetime的验证,时间格式化
|
JavaScript 前端开发
【JavaScript】Date对象(创建时间对象、常用Date方法总结)
【JavaScript】Date对象(创建时间对象、常用Date方法总结)
155 0
C# string格式的日期时间转为DateTime类型
C# string格式的日期时间转为DateTime类型
389 0
|
Web App开发 JavaScript iOS开发
Date 对象
Date 对象
146 0
|
JSON Unix 数据库
25、Date 对象
Date对象可以作为普通函数直接调用,返回一个代表当前时间的字符串。
128 0