C#WPFDateTime初始化,赋值DateTime如何赋值
DateTime dt = new DateTime(2008, 5, 1, 8, 30, 52,001); //正确
DateTime dt = new DateTime();
dt.Year = 2008;//错误  Year 是只读属性,DateTime 不根据该⽅式给年⽉⽇等赋值。
可以⽤强转,
DateTime d = DateTime.Parse("2010-2-12");
或者是创建新的实例
DateTime dd = new DateTime(2010, 2, 12);
new DateTime(2010, 2, 12);
这个的参数有好多,也可以⾃定义⼩时,分,秒,
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式输出s
Console.WriteLine(dt.ToString());    //  26/11/2009 AM 11:21:30
Console.WriteLine(dt.ToFileTime().ToString());  //  129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToFileTimeUtc().ToString());  //    129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToLocalTime().ToString());  //      26/11/2009 AM 11:21:30
// Converts the value of the current System.DateTime object to local time.
Console.WriteLine(dt.ToLongDateString().ToString());  //      2009年11⽉26⽇
Console.WriteLine(dt.ToLongTimeString().ToString());  //      AM 11:21:30
Console.WriteLine(dt.ToOADate().ToString());  //      40143.4732731597
Console.WriteLine(dt.ToShortDateString().ToString());  //    26/11/2009
Console.WriteLine(dt.ToShortTimeString().ToString());  //    AM 11:21
Console.WriteLine(dt.ToUniversalTime().ToString());  //      26/11/2009 AM 3:21:30
Console.WriteLine(dt.Year.ToString());  //        2009
Console.WriteLine(dt.Date.ToString());  //        26/11/2009 AM 12:00:00
Console.WriteLine(dt.DayOfWeek.ToString());  //      Thursday
Console.WriteLine(dt.DayOfYear.ToString());  //      330
Console.WriteLine(dt.Hour.ToString());      //        11
Console.WriteLine(dt.Millisecond.ToString());  //    801        (毫秒)
Console.WriteLine(dt.Minute.ToString());  //      21
Console.WriteLine(dt.Month.ToString());  //      11
Console.WriteLine(dt.Second.ToString());  //      30
Console.WriteLine(dt.Ticks.ToString());  //      633948312908014024
Console.WriteLine(dt.TimeOfDay.ToString());  //      12:29:51.5181524
// Gets the time of day for this instance.
// 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight. Console.WriteLine(dt.ToString());    //    26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddYears(1).ToString());    //        26/11/2010 PM 12:29:51
Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51
Console.WriteLine(dt.AddHours(1.1).ToString());    //      26/11/2009 PM 1:35:51
Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51
Console.WriteLine(dt.AddSeconds(1.1).ToString());    //    26/11/2009 PM 12:29:52
Console.WriteLine(dt.AddMinutes(1.1).ToString());    //    26/11/2009 PM 12:30:57
Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:51
Console.WriteLine(dt.CompareTo(dt).ToString());    //      0
Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString());    // 加上⼀个时间段
(注:
System.TimeSpan为⼀个时间段,构造函数如下
public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.
//nanosecond:⼗亿分之⼀秒  new TimeSpan(10,000,000)        为⼀秒
public TimeSpan(int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
Console.WriteLine(dt.Equals("2005-11-6 16:11:04").ToString());    //        False
Console.WriteLine(dt.Equals(dt).ToString());    //      True
Console.WriteLine(dt.GetHashCode().ToString());    //      1103291775
Console.WriteLine(dt.GetType().ToString());    //      System.DateTime
Console.WriteLine(dt.GetTypeCode().ToString());    //      DateTime
long Start = Environment.TickCount;  //单位是毫秒
long End = Environment.TickCount;
Console.WriteLine("Start is : "+Start);
Console.WriteLine("End is : "+End);
Console.WriteLine("The Time is {0}",End-Start);
Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString());    //2009-11-26T13:29:06 Console.Writ
eLine(dt.GetDateTimeFormats('t')[0].ToString());    //PM 1:29
Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString());    //2009年11⽉
Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString());    //2009年11⽉26⽇
Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString());    //星期四, 26 ⼗⼀⽉, 2009 Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString());    //26 ⼗⼀⽉, 2009
Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString());    //星期四 2009 11 26 Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString());    //26 ⼗⼀⽉
Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString());    //2009年11⽉26⽇ PM 1:29 Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString());    //26/11/2009 PM 1:29 Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString());    //Thu, 26 Nov 2009 13:29:06 GMT (注:
常⽤的⽇期时间格式:
格式说明输出格式
d 精简⽇期格式 MM/dd/yyyy
D 详细⽇期格式 dddd, MMMM dd, yyyy
f  完整格式    (lon
g date + short time) dddd, MMMM dd, yyyy HH:mm
F 完整⽇期时间格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss
g ⼀般格式 (short date + short time) MM/dd/yyyy HH:mm
G ⼀般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M ⽉⽇格式 MMMM dd
s 适中⽇期时间格式 yyyy-MM-dd HH:mm:ss
t 精简时间格式 HH:mm
T 详细时间格式 HH:mm:ss
)
Console.WriteLine(string.Format("{0:d}", dt));    //28/12/2009
Console.WriteLine(string.Format("{0:D}", dt));    //2009年12⽉28⽇
Console.WriteLine(string.Format("{0:f}", dt));    //2009年12⽉28⽇ AM 10:29
Console.WriteLine(string.Format("{0:F}", dt));    //2009年12⽉28⽇ AM 10:29:18
Console.WriteLine(string.Format("{0:g}", dt));    //28/12/2009 AM 10:29
Console.WriteLine(string.Format("{0:G}", dt));    //28/12/2009 AM 10:29:18
Console.WriteLine(string.Format("{0:M}", dt));    //28 ⼗⼆⽉
Console.WriteLine(string.Format("{0:R}", dt));    //Mon, 28 Dec 2009 10:29:18 GMT
Console.WriteLine(string.Format("{0:s}", dt));    //2009-12-28T10:29:18
Console.WriteLine(string.Format("{0:t}", dt));    //AM 10:29
Console.WriteLine(string.Format("{0:t}", dt));    //AM 10:29
Console.WriteLine(string.Format("{0:T}", dt));    //AM 10:29:18
Console.WriteLine(string.Format("{0:u}", dt));    //2009-12-28 10:29:18Z
Console.WriteLine(string.Format("{0:U}", dt));    //2009年12⽉28⽇ AM 2:29:18
Console.WriteLine(string.Format("{0:Y}", dt));    //2009年12⽉
Console.WriteLine(string.Format("{0}", dt));    //28/12/2009 AM 10:29:18
Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt));    //200912281029182047
计算2个⽇期之间的天数差
DateTime dt1 = Convert.ToDateTime("2007-8-1");
DateTime dt2 = Convert.ToDateTime("2007-8-15");
TimeSpan span = dt2.Subtract(dt1);
int dayDiff = span.Days ;
计算某年某⽉的天数
int days = DateTime.DaysInMonth(2009, 8);
days = 31;
给⽇期增加⼀天、减少⼀天
DateTime dt =DateTime.Now;
dt.AddDays(1); //增加⼀天 dt本⾝并不改变
dt.AddDays(-1);//减少⼀天 dt本⾝并不改变
DateTime  dt1  =  DateTime.Now;
DateTime  dt2  =  DateTime.Now.AddDays(-7);
TimeSpan  ts  =  dt1  -  dt2;
int  days  =  ts.Days;  //dt1和dt2相差多少天
eg2.
//编辑⽉份和系统登录⽉份是否相等
if(Convert.ToDateTime(DateTime.Now.ToShortDateString()).Month==Convert.ToDateTime(this.GetQueryString("sdate")).Month)    {
TimeSpan  ts=Convert.ToDateTime(DateTime.Now.ToShortDateString())-Convert.ToDateTime(this.GetQueryString("sdate"));      iDays=ts.Days;
//登录系统的前三天可以编辑
if(iDays<4)
{
Submiter submit = new Submiter(this,"tt");
this.iseretail.SelectedValue=this.GetQueryString("rid");
this.InputSelect1.WhereCondition=" gxlshd='"+this.GetQueryString("rid")+"'";
if(this.GetQueryString("workid")!="")
this.InputSelect1.SelectedValue=this.GetQueryString("workid");
submit.InitInsertMode();
SetDataGrid();
}
eg3.C#中计算两个时间的差
/// <summary>
/// 计算两个⽇期的时间间隔
/// </summary>
/// <param name="DateTime1">第⼀个⽇期和时间</param>
/
// <param name="DateTime2">第⼆个⽇期和时间</param>
/// <returns></returns>
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
dateDiff = ts.Days.ToString()+"天"
+ ts.Hours.ToString()+"⼩时"
+ ts.Minutes.ToString()+"分钟"
+ ts.Seconds.ToString()+"秒";
return dateDiff;
}说明:
1.DateTime值类型代表了⼀个从公元0001年1⽉1⽇0点0分0秒到公元9999年12⽉31⽇23点59分59秒之间的具体⽇期时刻。因此,你可以⽤DateTime值类型来描述任
2.TimeSpan值包含了许多属性与⽅法,⽤于访问或处理⼀个TimeSpan值
下⾯的列表涵盖了其中的⼀部分:
Add:与另⼀个TimeSpan值相加。
Days:返回⽤天数计算的TimeSpan值。
Duration:获取TimeSpan的绝对值。
writeline函数
Hours:返回⽤⼩时计算的TimeSpan值
Milliseconds:返回⽤毫秒计算的TimeSpan值。
Minutes:返回⽤分钟计算的TimeSpan值。
Negate:返回当前实例的相反数。
Seconds:返回⽤秒计算的TimeSpan值。
Subtract:从中减去另⼀个TimeSpan值。
Ticks:返回TimeSpan值的tick数。
TotalDays:返回TimeSpan值表⽰的天数。
TotalHours:返回TimeSpan值表⽰的⼩时数。
TotalMilliseconds:返回TimeSpan值表⽰的毫秒数。
TotalMinutes:返回TimeSpan值表⽰的分钟数。
TotalSeconds:返回TimeSpan值表⽰的秒数。
见:
#region 程序集 mscorlib.dll, v4.0.0.0
// C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion
using System.Globalization;
using System.Runtime;
using System.Runtime.Serialization;
using System.Security;
namespace System
{
// 摘要:
//    表⽰时间上的⼀刻,通常以⽇期和当天的时间表⽰。
[Serializable]
public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
{
// 摘要:
//    表⽰ System.DateTime 的最⼤可能值。此字段为只读。
public static readonly DateTime MaxValue;
//

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。