//獲取日期+時(shí)間
成都創(chuàng)新互聯(lián)擁有10年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),對(duì)于網(wǎng)頁設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、app軟件開發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、域名注冊(cè)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。
DateTime.Now.ToString();
DateTime.Now.ToLocalTime().ToString();
//獲取日期
DateTime.Now.ToLongDateString().ToString();
DateTime.Now.ToShortDateString().ToString();
DateTime.Now.ToString("yyyy-MM-dd");
DateTime.Now.Date.ToString();
//獲取時(shí)間
DateTime.Now.ToLongTimeString().ToString();
DateTime.Now.ToShortTimeString().ToString();
DateTime.Now.ToString("hh:mm:ss");
DateTime.Now.TimeOfDay.ToString();
//獲取當(dāng)前日期是星期幾,根據(jù)取得的英文單詞返回漢字
dt = DateTime.Today.DayOfWeek.ToString();
switch (dt)
{
case "Monday":
week ="星期一";
break ;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thursday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
//今天 DateTime.Now.Date.ToShortDateString();
//昨天 的 DateTime.Now.AddDays(-1).ToShortDateString();
//明天 的 DateTime.Now.AddDays(1).ToShortDateString();
//本周(注意這里的每一周是從周日始至周六止)
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
//上周,上周就是本周再減去7天
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
//下周 本周再加上7天
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
//本月 本月的第一天是1號(hào),最后一天就是下個(gè)月一號(hào)再減一天。
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天
另一種方法:
DateTime now = DateTime.Now;
DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天
DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后一天
PS:
DateTime.Now.DayOfWeek.ToString();//英文星期顯示,Wednesday
(int)DateTime.Now.DayOfWeek 數(shù)字,若是周三,結(jié)果對(duì)應(yīng)為3
DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //中文星期顯示
DateTime.Now.ToString("dddd");//中文星期顯示
DateTime.Now.ToString("dddd,MMMM,dd ,yyyy", new System.Globalization.DateTimeFormatInfo());//顯示日期格式Friday,July, 01,2009
DateTime.Now.ToString("dddd,dd MMMM,yyyy") //輸出 星期三,30 一月,2008
//其他
DateTime.ToFileTime().ToString();
DateTime.Now.ToFileTimeUtc().ToString();
DateTime.Now.ToOADate().ToString();
DateTime.Now.ToUniversalTime().ToString();
DateTime.Now.Year.ToString();
DateTime.Now.Month.ToString();
DateTime.Now.DayOfWeek.ToString(); 獲取星期
DateTime.Now.DayOfYear.ToString(); 獲取第幾天
DateTime.Now.Hour.ToString();
DateTime.Now.Minute.ToString();
DateTime.Now.Second.ToString();
//n為一個(gè)數(shù),可以數(shù)整數(shù),也可以事小數(shù)
dt.AddYears(n).ToString();
dt.AddDays(n).ToString();
dt.AddHours(n).ToString();
dt.AddMonths(n).ToString();
dt.AddSeconds(n).ToString();
dt.AddMinutes(n).ToString();
SQL語句使用時(shí)間和日期的函數(shù)
getdate():獲取系統(tǒng)當(dāng)前時(shí)間
dateadd(datepart,number,date):計(jì)算在一個(gè)時(shí)間的基礎(chǔ)上增加一個(gè)時(shí)間后的新時(shí)間值,比如:dateadd(yy,30,getdate())
datediff(datepart,startdate,enddate):計(jì)算兩個(gè)時(shí)間的差值,比如:datediff(yy,getdate(),'2008-08-08')
dataname(datepart,date):獲取時(shí)間不同部分的值,返回值為字符串
datepart(datepart,date):和datename相似,只是返回值為整型
day(date):獲取指定時(shí)間的天數(shù)
month(date):獲取指定時(shí)間的月份
year(date):獲取指定時(shí)間的年份
select year(getdate())