DateTime offen use (常用功能)
请移步到: Date and Time 日期时间 – 开发中的基础知识
更新 : 2017-10-11
https://docs.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql
public partial class HTML4_date_time_js_Default : System.Web.UI.Page { public static int getww(DateTime time) { // Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll // be the same week# as whatever Thursday, Friday or Saturday are, // and we always get those right DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time); if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday) { time = time.AddDays(3); } // Return the week of our adjusted day return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); } public static int getquarter(DateTime date) { if (date.Month >= 1 && date.Month <= 3) return 1; else if (date.Month >= 4 && date.Month <= 6) return 2; else if (date.Month >= 7 && date.Month <= 9) return 3; else return 4; } protected void Page_Load(object sender, EventArgs e) { //当前时间 string datetime = DateTime.Now.ToString("MM_dd_yyyy") + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Millisecond.ToString(); //set a date DateTime dt_date = new DateTime(2013, 10, 07, 00, 05, 00, 00); //add day / month / year DateTime dt_last_day = dt_date.AddDays(-1); // to string string last_day = dt_last_day.ToString("yyyy-MM-dd"); //get 星期几 result : monday string day = (Convert.ToDateTime(dt_last_day).DayOfWeek.ToString()); //get year string year = dt_last_day.Year.ToString(); //get month string month = dt_last_day.Month.ToString(); //get jam string jam = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt", new CultureInfo("en-US")); // output : 2012-06-24 02:40:23 AM jam = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff", new CultureInfo("en-US")); // output : 2012-06-24 02:40:23:256 国际时间 // 获取当月最后一天 result : 29 int last_day_of_month = System.DateTime.DaysInMonth(2000, 02); //算第几个 work week string ww = getww(dt_last_day).ToString(); //get 季度 string quarter = getquarter(dt_last_day).ToString(); //时间差 DateTime today = DateTime.Now; //today DateTime last_night = DateTime.Now.AddDays(-1); //last night //compare 大小 int result = DateTime.Compare(today, last_night); //if result < 0 (A early than B) para 1 比较早 , = 0 同一天 ,>0 就是迟咯 TimeSpan chabie = today.Subtract(last_night); //对比时间差 double double_chabie = chabie.TotalMilliseconds; //convert to milisecond double_chabie = Math.Round(double_chabie, 0); // round up to 没有小数点 double_chabie = double_chabie / 1000 / 60 / 60 / 24; // formula to unit 天 // String to DateTime String MyString = "1999-09-01 21:34 PM"; //MyString = "1999-09-01 21:34 p.m."; //Depends on your regional settings DateTime MyDateTime; MyDateTime = new DateTime(); MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", new CultureInfo("en-US")); //DateTime to String MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00); MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt", new CultureInfo("en-US")); // timezone 处理 DateTime dt = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now, TimeZoneInfo.Local); //当前本地时间 string a = dt.ToString("yyyy-MM-dd hh:mm:ss tt", new CultureInfo("en-US")); dt = TimeZoneInfo.ConvertTimeFromUtc(dt, TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time")); //convert to 指定的区域 a = dt.ToString("yyyy-MM-dd hh:mm:ss tt", new CultureInfo("en-US")); a = ""; ReadOnlyCollection<TimeZoneInfo> lst = TimeZoneInfo.GetSystemTimeZones(); //获取所有可以convert to 的list 比如 Singapore Standard Time foreach (TimeZoneInfo tzi in lst) { a = a == "" ? tzi.Id : a + "\r\n" + tzi.Id; } //在某个时间内 //方便下次调用 DateTime now = DateTime.Now; string day123 = now.DayOfWeek.ToString(); if (day123 == "Monday") { DateTime start = DateTime.ParseExact("00:00:00", "HH:mm:ss", new CultureInfo("en-US")); DateTime end = DateTime.ParseExact("00:15:00", "HH:mm:ss", new CultureInfo("en-US")); if (DateTime.Compare(now, start) >= 0 && DateTime.Compare(now, end) <= 0) { //do something } } //指定一个时间内连续触发,但是成功后就不做了(cache up 成功) 方便下次调用 DateTime start1 = new DateTime(now.Year, now.Month, now.Day, now.Hour, 00, 00, 000); DateTime end1 = new DateTime(now.Year, now.Month, now.Day, now.Hour, 05, 00, 000); if (DateTime.Compare(now, start1) >= 0 && DateTime.Compare(now, end1) < 0) { if (Cache["key"] == null) { double gap = end1.Subtract(now).TotalMilliseconds; Cache.Add("key", "x", null, DateTime.Now.AddMilliseconds(gap), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); } } } }
想判断一个时间有没有在几点到几点之间
TimeSpan startTime = new TimeSpan(7,0,0); TimeSpan endTime = new TimeSpan(2, 59, 0); if (DateTime.Now.TimeOfDay >= startTime && DateTime.Now.TimeOfDay <= endTime) { //in range } else { //not in range. }
更新 : 2015-11-19
DateTime xx = new DateTime(2015, 1, 30).AddMonths(1);
c# 的 AddMonths 和 js 的都是用 setMonth 的概念,区别是js 会因为day超出当月份时“自动”往后加多几天,导致月份跟可能随着改变, C#则完全不会"自动".