在c# / ASP.net中我们可以通过使用DataTime这个类来获取当前的时间。通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04)、时间(12:12:12)、日期+时间(2008-09-04 12:11:10)等。<wbr></wbr>
<wbr></wbr>
//获取日期+时间<wbr></wbr>
DateTime.Now.ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 2008-9-4 20:02:10<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToLocalTime().ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 2008-9-4 20:12:12<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
//获取日期<wbr></wbr>
DateTime.Now.ToLongDateString().ToString();<wbr><wbr><wbr> // 2008年9月4日<wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToShortDateString().ToString();<wbr><wbr><wbr> // 2008-9-4<wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToString("yyyy-MM-dd");<wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 2008-09-04<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.Date.ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 2008-9-4 0:00:00<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 2008-9-4 20:12:12<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
//获取时间<wbr></wbr>
DateTime.Now.ToLongTimeString().ToString();<wbr><wbr><wbr> // 20:16:16<wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToShortTimeString().ToString();<wbr><wbr><wbr> // 20:16<wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToString("hh:mm:ss");<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 08:05:57<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.TimeOfDay.ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 20:33:50.7187500<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
//其他<wbr></wbr>
DateTime.ToFileTime().ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 128650040212500000<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToFileTimeUtc().ToString();<wbr><wbr><wbr> // 128650040772968750<wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToOADate().ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr> // 39695.8461709606<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.ToUniversalTime().ToString();<wbr><wbr><wbr> // 2008-9-4 12:19:14<wbr></wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
DateTime.Now.Year.ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 获取年份<wbr><wbr><wbr> // 2008<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.Month.ToString();<wbr><wbr><wbr><wbr><wbr> 获取月份<wbr><wbr><wbr> // 9<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.DayOfWeek.ToString();<wbr> 获取星期<wbr><wbr><wbr> // Thursday<wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.DayOfYear.ToString();<wbr> 获取第几天<wbr><wbr><wbr> // 248<wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.Hour.ToString();<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 获取小时<wbr><wbr><wbr> // 20<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.Minute.ToString();<wbr><wbr><wbr><wbr> 获取分钟<wbr><wbr><wbr> // 31<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
DateTime.Now.Second.ToString();<wbr><wbr><wbr><wbr> 获取秒数<wbr><wbr><wbr> // 45<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
//n为一个数,可以数整数,也可以事小数<wbr></wbr>
dt.AddYears(n).ToString();<wbr><wbr><wbr> //时间加n年<wbr></wbr></wbr></wbr></wbr>
dt.AddDays(n).ToString();<wbr><wbr><wbr> //加n天<wbr></wbr></wbr></wbr></wbr>
dt.AddHours(n).ToString();<wbr><wbr><wbr> //加n小时<wbr></wbr></wbr></wbr></wbr>
dt.AddMonths(n).ToString();<wbr><wbr><wbr> //加n个月<wbr></wbr></wbr></wbr></wbr>
dt.AddSeconds(n).ToString();<wbr><wbr><wbr> //加n秒<wbr></wbr></wbr></wbr></wbr>
dt.AddMinutes(n).ToString();<wbr><wbr><wbr> //加n分</wbr></wbr></wbr>