Let's go

获取(当前)日期型字段里的年、月、日

C#控制台测试

eg1:

  1 static void Main(string[] args)
  2         {
  3             //只取日期
  4             var date = DateTime.Now.ToShortDateString();
  5 
  6             //只取时间
  7             var time = DateTime.Now.ToLongTimeString();
  8 
  9             DateTime dt = DateTime.Now;
 10             ///测试一.
 11             ///
 12             dt.ToString();//2005-11-5 13:21:25
 13             dt.ToFileTime().ToString();//127756416859912816
 14             dt.ToFileTimeUtc().ToString();//127756704859912816
 15             dt.ToLocalTime().ToString();//2005-11-5 21:21:25
 16             dt.ToLongDateString().ToString();//2005年11月5日
 17             dt.ToLongTimeString().ToString();//13:21:25
 18             dt.ToOADate().ToString();//38661.5565508218
 19             dt.ToShortDateString().ToString();//2005-11-5
 20             dt.ToShortTimeString().ToString();//13:21
 21             dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
 22             dt.Year.ToString();//2005
 23             dt.Date.ToString();//2005-11-5 0:00:00
 24             dt.DayOfWeek.ToString();//Saturday
 25             dt.DayOfYear.ToString();//309
 26             dt.Hour.ToString();//13
 27             dt.Millisecond.ToString();//441
 28             dt.Minute.ToString();//30
 29             dt.Month.ToString();//11
 30             dt.Second.ToString();//28
 31             dt.Ticks.ToString();//632667942284412864
 32             dt.TimeOfDay.ToString();//13:30:28.4412864
 33             dt.ToString();//2005-11-5 13:47:04
 34             dt.AddYears(1).ToString();//2006-11-5 13:47:04
 35             dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
 36             dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
 37             dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
 38             dt.AddMonths(1).ToString();//2005-12-5 13:47:04
 39             dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
 40             dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
 41             dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
 42             dt.CompareTo(dt).ToString();//0
 43             //dt.Add(?).ToString();//问号为一个时间段
 44             dt.Equals("2005-11-6 16:11:04").ToString();//False
 45             dt.Equals(dt).ToString();//True
 46             dt.GetHashCode().ToString();//1474088234
 47             dt.GetDateTimeFormats('f')[0].ToString();//2018年7月6日 17:13
 48             dt.GetDateTimeFormats('g')[0].ToString();//2018/7/6 17:14
 49             dt.GetDateTimeFormats('D')[0].ToString();//2018年7月6日
 50             dt.GetDateTimeFormats('D')[1].ToString();//2018年7月6日, 星期五
 51             dt.GetDateTimeFormats('D')[2].ToString();//星期五, 2018年7月6日
 52             dt.GetDateTimeFormats('D')[3].ToString();//2018年7月6日
 53             dt.GetDateTimeFormats('D')[4].ToString();//2018年7月6日, 星期五
 54             dt.GetDateTimeFormats('M')[0].ToString();//7月6日
 55             dt.GetDateTimeFormats('r')[0].ToString();//Fri, 06 Jul 2018 17:18:28 GMT
 56             dt.GetDateTimeFormats('y')[0].ToString();//2018年7月
 57             dt.GetDateTimeFormats('t')[0].ToString();//17:21
 58             dt.GetDateTimeFormats('s')[0].ToString();//2018-07-06T17:22:25
 59             dt.GetTypeCode().ToString();//DateTime
 60             dt.GetType().ToString();//System.DateTime
 61 
 62             ///测试二.
 63             ///
 64             string.Format("{0:d}", dt);//2018/7/6
 65             string.Format("{0:D}", dt);//2018年7月6日
 66             string.Format("{0:f}", dt);//2005年11月5日 14:23
 67             string.Format("{0:F}", dt);//2005年11月5日 14:23:23
 68             string.Format("{0:g}", dt);//2005-11-5 14:23
 69             string.Format("{0:G}", dt);//2005-11-5 14:23:23
 70             string.Format("{0:M}", dt);//11月5日
 71             string.Format("{0:R}", dt);//Sat, 05 Nov 2005 14:23:23 GMT
 72             string.Format("{0:s}", dt);//2005-11-05T14:23:23
 73             string.Format("{0:t}", dt);//14:23
 74             string.Format("{0:T}", dt);//14:23:23
 75             string.Format("{0:u}", dt);//2005-11-05 14:23:23Z
 76             string.Format("{0:U}", dt);//2005年11月5日 6:23:23
 77             string.Format("{0:Y}", dt);//2005年11月
 78             string.Format("{0}", dt);//2005-11-5 14:23:23
 79             string.Format("{0:yyyyMMddHHmmssffff}", dt);
 80 
 81             //计算2个日期之间的天数差
 82             DateTime dt1 = Convert.ToDateTime("2007-8-1");
 83             DateTime dt2 = Convert.ToDateTime("2007-8-15");
 84             TimeSpan span = dt2.Subtract(dt1);
 85             int dayDiff = span.Days + 1;
 86 
 87             //计算某年某月的天数
 88             int days = DateTime.DaysInMonth(2018, 6);
 89 
 90             //给日期增加一天、减少一天
 91             dt.AddDays(1); //增加一天
 92             dt.AddDays(-1);//减少一天
 93 
 94             //获取当前的年日月
 95             //DateTime.Now.Year;
 96             //DateTime.Now.Date.Month;
 97             //DateTime.Now.Date.Day;
 98 
 99 
100             Console.WriteLine();
101             //Console.WriteLine(date);
102             Console.ReadKey();
103         }
View Code

 eg2:

  1 获得当前系统时间: DateTime dt = DateTime.Now;
  2 Environment.TickCount可以得到“系统启动到现在”的毫秒值
  3 DateTime now = DateTime.Now;
  4 Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式输出s
  5 Console.WriteLine(dt.ToString());    //  26/11/2009 AM 11:21:30
  6 Console.WriteLine(dt.ToFileTime().ToString());   //   129036792908014024
  7 // Converts the value of the current System.DateTime object to a Windows file time
  8 Console.WriteLine(dt.ToFileTimeUtc().ToString());  //     129036792908014024
  9 // Converts the value of the current System.DateTime object to a Windows file time
 10 Console.WriteLine(dt.ToLocalTime().ToString());   //       26/11/2009 AM 11:21:30
 11 // Converts the value of the current System.DateTime object to local time.
 12 Console.WriteLine(dt.ToLongDateString().ToString());   //      2009年11月26日
 13 Console.WriteLine(dt.ToLongTimeString().ToString());  //      AM 11:21:30
 14 Console.WriteLine(dt.ToOADate().ToString());   //      40143.4732731597
 15 Console.WriteLine(dt.ToShortDateString().ToString());   //     26/11/2009
 16 Console.WriteLine(dt.ToShortTimeString().ToString());   //     AM 11:21
 17 Console.WriteLine(dt.ToUniversalTime().ToString());   //       26/11/2009 AM 3:21:30
 18 Console.WriteLine(dt.Year.ToString());   //        2009
 19 Console.WriteLine(dt.Date.ToString());   //        26/11/2009 AM 12:00:00
 20 Console.WriteLine(dt.DayOfWeek.ToString());  //       Thursday
 21 Console.WriteLine(dt.DayOfYear.ToString());   //       330
 22 Console.WriteLine(dt.Hour.ToString());       //        11
 23 Console.WriteLine(dt.Millisecond.ToString());   //     801        (毫秒)
 24 Console.WriteLine(dt.Minute.ToString());   //      21
 25 Console.WriteLine(dt.Month.ToString());   //       11
 26 Console.WriteLine(dt.Second.ToString());   //      30
 27 Console.WriteLine(dt.Ticks.ToString());   //       633948312908014024
 28  
 29 Console.WriteLine(dt.TimeOfDay.ToString());   //       12:29:51.5181524
 30 // Gets the time of day for this instance.
 31 // 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight.
 32 Console.WriteLine(dt.ToString());     //     26/11/2009 PM 12:29:51
 33 Console.WriteLine(dt.AddYears(1).ToString());    //         26/11/2010 PM 12:29:51
 34 Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51
 35 Console.WriteLine(dt.AddHours(1.1).ToString());    //       26/11/2009 PM 1:35:51
 36 Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51
 37 Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51
 38 Console.WriteLine(dt.AddSeconds(1.1).ToString());    //     26/11/2009 PM 12:29:52
 39 Console.WriteLine(dt.AddMinutes(1.1).ToString());    //     26/11/2009 PM 12:30:57
 40 Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:51
 41 Console.WriteLine(dt.CompareTo(dt).ToString());    //       0
 42 Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString());    // 加上一个时间段
 43 (注:
 44 System.TimeSpan为一个时间段,构造函数如下
 45 public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.
 46                            //nanosecond:十亿分之一秒   new TimeSpan(10,000,000)        为一秒
 47 public TimeSpan(int hours, int minutes, int seconds);
 48 public TimeSpan(int days, int hours, int minutes, int seconds);
 49 public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
 50  51 Console.WriteLine(dt.Equals("2005-11-6 16:11:04").ToString());     //        False
 52 Console.WriteLine(dt.Equals(dt).ToString());    //      True
 53 Console.WriteLine(dt.GetHashCode().ToString());    //       1103291775
 54 Console.WriteLine(dt.GetType().ToString());    //       System.DateTime
 55 Console.WriteLine(dt.GetTypeCode().ToString());    //       DateTime
 56   
 57 long Start = Environment.TickCount;   //单位是毫秒
 58 long End = Environment.TickCount;
 59 Console.WriteLine("Start is : "+Start);
 60 Console.WriteLine("End is : "+End);
 61 Console.WriteLine("The Time is {0}",End-Start);
 62 Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString());    //2009-11-26T13:29:06
 63 Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString());    //PM 1:29
 64 Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString());    //2009年11月
 65 Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString());    //2009年11月26日
 66 Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString());    //星期四, 26 十一月, 2009
 67 Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString());    //26 十一月, 2009
 68 Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString());    //星期四 2009 11 26
 69 Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString());    //26 十一月
 70 Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString());    //2009年11月26日 PM 1:29
 71 Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString());    //26/11/2009 PM 1:29
 72 Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString());    //Thu, 26 Nov 2009 13:29:06 GMT
 73 (注:
 74 常用的日期时间格式:
 75 格式 说明      输出格式 
 76 d 精简日期格式 MM/dd/yyyy 
 77 D 详细日期格式 dddd, MMMM dd, yyyy 
 78 f  完整格式    (long date + short time) dddd, MMMM dd, yyyy HH:mm 
 79 F 完整日期时间格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss 
 80 g 一般格式 (short date + short time) MM/dd/yyyy HH:mm 
 81 G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss 
 82 m,M 月日格式 MMMM dd 
 83 s 适中日期时间格式 yyyy-MM-dd HH:mm:ss 
 84 t 精简时间格式 HH:mm 
 85 T 详细时间格式 HH:mm:ss
 86 )
 87  
 88 Console.WriteLine(string.Format("{0:d}", dt));    //28/12/2009
 89 Console.WriteLine(string.Format("{0:D}", dt));    //2009年12月28日
 90 Console.WriteLine(string.Format("{0:f}", dt));    //2009年12月28日 AM 10:29
 91 Console.WriteLine(string.Format("{0:F}", dt));    //2009年12月28日 AM 10:29:18
 92 Console.WriteLine(string.Format("{0:g}", dt));    //28/12/2009 AM 10:29
 93 Console.WriteLine(string.Format("{0:G}", dt));    //28/12/2009 AM 10:29:18
 94 Console.WriteLine(string.Format("{0:M}", dt));    //28 十二月
 95 Console.WriteLine(string.Format("{0:R}", dt));    //Mon, 28 Dec 2009 10:29:18 GMT
 96 Console.WriteLine(string.Format("{0:s}", dt));    //2009-12-28T10:29:18
 97 Console.WriteLine(string.Format("{0:t}", dt));    //AM 10:29
 98 Console.WriteLine(string.Format("{0:T}", dt));    //AM 10:29:18
 99 Console.WriteLine(string.Format("{0:u}", dt));    //2009-12-28 10:29:18Z
100 Console.WriteLine(string.Format("{0:U}", dt));    //2009年12月28日 AM 2:29:18
101 Console.WriteLine(string.Format("{0:Y}", dt));    //2009年12月
102 Console.WriteLine(string.Format("{0}", dt));    //28/12/2009 AM 10:29:18
103 Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt));    //200912281029182047
104  
105 计算2个日期之间的天数差
106 DateTime dt1 = Convert.ToDateTime("2007-8-1");    
107 DateTime dt2 = Convert.ToDateTime("2007-8-15");   
108 TimeSpan span = dt2.Subtract(dt1);              
109 int dayDiff = span.Days ;                    
110  
111 计算某年某月的天数
112 int days = DateTime.DaysInMonth(2009, 8);       
113 days = 31;                                      
114  
115 给日期增加一天、减少一天
116 DateTime dt =DateTime.Now;
117 dt.AddDays(1); //增加一天 dt本身并不改变
118 dt.AddDays(-1);//减少一天 dt本身并不改变
View Code

 eg3:

转换秒为分秒

 answerAverageTime = TimeSpan.FromSeconds(u.answerAverageTime).ToString(@"mm\:ss"),

 获取数据库列表时,对时间格式进行处理

1                                              var demolList = demo.Entities
2                                             .OrderByDescending(a => a.addTime)
3                                             .ToList()
4                                             .Select(b => new
5                                             {
6                                                 addTime = Convert.ToDateTime(b.addTime).ToString("yyyy-MM-dd HH:mm"),
7                                             })
8                                             .ToList();                                                            
View Code

 对格式进行处理

 1 demo= u.demoDouHao.Replace(",", ""), 

 

一,jq获取两日期的月数

//返回两个日期相差的月数
function MonthsBetw(date1, date2) {
//用-分成数组
date1 = date1.split("-");
date2 = date2.split("-");
//获取年,月数
var year1 = parseInt(date1[0]),
month1 = parseInt(date1[1]),
year2 = parseInt(date2[0]),
month2 = parseInt(date2[1]),
//通过年,月差计算月份差
months = (year2 - year1) * 12 + (month2 - month1) + 1;
return months;
}

 二,两日期进行比较

//必须大于当前日期
var ayear = new Date().getFullYear();
var amonth = new Date().getMonth() + 1;
var aday = new Date().getDate();
var jhDate = $("#invoicingDate").val();
var adate = new Date((ayear + "-" + amonth + "-" + aday).replace("-", "/"));
var aadate = new Date(jhDate.replace("-", "/"));
if (aadate < adate) {
    alert("计划日期必须大于等于当前日期");
    return false;
}

公共方法

//返回比较日期的大小
function CompareDate(d1, d2) {
    return ((new Date(d1.replace(/-/g, "\/"))) < (new Date(d2.replace(/-/g, "\/"))));
}
posted @ 2018-07-06 17:39  chenze  阅读(1552)  评论(0编辑  收藏  举报
有事您Q我