计算日期差并转换为周 [英] Calculate the Date Difference and Convert into Week

查看:98
本文介绍了计算日期差并转换为周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想区分两个日期,然后将其转换为日期
例如在10/28/2010中添加7天,因此下一个日期是2010年11月4日,那么如何计算日期?


我的意思是,如果最后一个月,如果我加8,我们选择第29个,那么结果日期为36,但36是不可能的日期.


下一个队列.

如何计算从开始日期到下一个日期的一年中的季度,例如第一个季度是从4月到8月.所以季度的第一个日期和季度的最后一个日期

请帮忙.

I want to take a difference between two date and convert it into the date
like in 10/28/2010 add 7 days so the next date is 11/04/2010 so how to calculate the date ?


I mean that if a month at the End and we select the 29th if I add 8 so the result 36 but 36 is not possible date.


Next Que.

how to calculate the quarter of the year from the starting date to Next Date like first Quarter is Start from April to Aug. so the first date of quarter and the Last Date of Quarter

Please help.
thanks in advance.

推荐答案

要计算季度,可以使用 .NET时间周期库:
To calculate the quarter you can use the Time Period Library for .NET:
// ----------------------------------------------------------------------
public static void ShowQuarterInfo()
{
  ShowQuarterInfo( DateTime.Now );
} // ShowQuarterInfo

// ----------------------------------------------------------------------
public static void ShowQuarterInfo( DateTime moment )
{
  // working quarter
  Quarter quarter = new Quarter( moment );
  Console.WriteLine( "Quarter start: " + quarter.FirstDayStart );
  Console.WriteLine( "Quarter end: " + quarter.LastDayStart );

  // previous quarter
  Quarter previousQuarter = quarter.GetPreviousQuarter();
  Console.WriteLine( "Previous Quarter start: " + previousQuarter.FirstDayStart );
  Console.WriteLine( "Previous Quarter end: " + previousQuarter.LastDayStart );

  // next quarter
  Quarter nextQuarter = quarter.GetNextQuarter();
  Console.WriteLine( "Next Quarter start: " + nextQuarter.FirstDayStart );
  Console.WriteLine( "Next Quarter end: " + nextQuarter.LastDayStart );
} // ShowQuarterInfo



您可以在文章 .NET的时间段库 [



You will find more samples in the article Time Period Library for .NET[^].

Cheers, Jani.


使用AddDays Like

Use AddDays Like

DateTime mydt = DateTime.Today.Date.AddDays(7);


亲爱的,

U无法以日期形式区分两个日期.
但您可以获得以秒,分钟,小时,天,月,年为单位的日期差.
因此,为了得到两个日期之间的差b/n,我们需要进行级联,但是如果我们采用同一天,同一月和同一年的差,那么在每种情况下我们都不会以日期格式coz约会,这就是问题0表示天,月和年.看起来像00 \ 00 \ 0000.根据您的选择,任何东西都可以为零.因此,如果我们加上7天,我们将无法获得正确的结果

E.G
选择dateadd(d,+ 5,datediff(d,getdate(),getdate()+ 1))
这给出了1900-01-07 00:00:00.000
coz
选择datediff(d,getdate(),getdate()+ 1)
它给出1.
如果再加上7天,我们将得到
1900-01-07
Dear,

U can''t make the difference between two dates in date form.
but u can get date difference in sec,min,hr,days,months,years.
So In order to get difference b/n two dates we need to do concatenation,but the things is this if we take difference of same day,same month and same year then we will not date in date form coz in each case we will get 0 for days,months and years.That will look like 00\00\0000. Any thing can be zero as per your choice.So if we add 7 days then we will not get proper result

E.G
select dateadd(d,+5, datediff(d,getdate(),getdate()+1) )
this gives 1900-01-07 00:00:00.000
coz
select datediff(d,getdate(),getdate()+1)
it gives 1.
if we add 7 days then we will get
1900-01-07


这篇关于计算日期差并转换为周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆