码迷,mamicode.com
首页 > 其他好文 > 详细

C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期

时间:2014-05-21 23:32:51      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   http   color   

C# 时间计算
 
 今天、昨天、前天、明天
  class Program
    {
        static void Main(string[] args)
        {
            DateTime dt1=GetTodayZeroTime(DateTime.Now);
            Console.WriteLine("今天零点:{0}",dt1.ToString() );
            DateTime dt2 = GetYestodayZeroTime(DateTime.Now);
            Console.WriteLine("昨天零点:{0}", dt2.ToString());
            DateTime dt3 = GetDayBeforYestodayZeroTime(DateTime.Now);
            Console.WriteLine("前天零点:{0}", dt3.ToString());
            DateTime dt4 = GetTomorrowZeroTime(DateTime.Now);
            Console.WriteLine("明天零点:{0}", dt4.ToString());
            Console.ReadLine();

        }

        private static DateTime GetTodayZeroTime(DateTime datetime)
        {
            return new DateTime(datetime.Year, datetime.Month, datetime.Day);
        }
        private static DateTime GetYestodayZeroTime(DateTime datetime)
        {
            TimeSpan timespan = new TimeSpan(1, 0, 0, 0);
            DateTime yesdt = datetime.Subtract(timespan);
            return new DateTime(yesdt.Year, yesdt.Month, yesdt.Day);
        }
        private static DateTime GetDayBeforYestodayZeroTime(DateTime datetime)
        {
            TimeSpan timespan = new TimeSpan(2, 0, 0, 0);
            DateTime yesdt = datetime.Subtract(timespan);
            return new DateTime(yesdt.Year, yesdt.Month, yesdt.Day);
        }
        private static DateTime GetTomorrowZeroTime(DateTime datetime)
        {
            TimeSpan timespan = new TimeSpan(1, 0, 0, 0);
            DateTime yesdt = datetime.Add(timespan);
            return new DateTime(yesdt.Year, yesdt.Month, yesdt.Day);
        }
    }
 
本文来自网络上朋友们的帖子,这里进行了一个汇总

取一个月的最后一天日期

int   yy   =   2005; 
int   mm   =   11; 
int   Lastday   =   DateTime.DaysInMonth(yy,   mm);  

另一种方法

int   year   =   2005; 
int   month   =   11; 
DateTime   dt   =   new   DateTime(year,month,1).AddMonths(1).AddDays(-1);

取某一天所在周的最后一天日期

String   date   =   "2007-09-27 "; 
SimpleDateFormat   sdf   =   new   SimpleDateFormat( "yyyy-MM-dd "); 
Calendar   calendar   =   new   GregorianCalendar(); 
calendar.setTime(sdf.parse(date)); 
calendar.set(Calendar.DAY_OF_WEEK,   calendar.getMaximum(Calendar.DAY_OF_WEEK)); 
System.out.println(sdf.format(calendar.getTime())); 
 
时间比较:
 
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
时间比较:
    while (DateTime.Compare(dtBegin, dtEnd) <= 0)
            {
                // 
                //循环主体
                 //    
                dtBegin = dtBegin.AddHours(1);           //时间相加
            }
 时间相减:
  TimeSpan ts = endtime.Subtract(begintime).Duration();

C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期,布布扣,bubuko.com

C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期

标签:style   blog   class   c   http   color   

原文地址:http://www.cnblogs.com/amyeeq/p/3739055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!