C # About the date of the date!

xiaoxiao2021-03-06  78

Topic: How many months A calculation of how difference between two DateTime variables: yzxdc (yzxdc) Grade: credit value: 93 belongs to the community: .NET C # technical problems Points: 50 Replies: 26 Time: 2004-9-9 12:15:48

How do I calculate how many months between two DateTime variables? Use Subtract only converted to the sky

Reply to: The123 (I have to listen to me in my site) () Reputation: 100 2004-9-9 12:20:22 Score: 0

DateTime DT1, DT2;

DT1.MONTH-DT2.MONTH

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 12:21:48 Score: 20

Timespan TS = DateTime1-datetime2;

Ts.TotalDays is the number of days of different dates, and the specific month is calculated.

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 12:22:48 Score: 0

Reply to: The123 (I have to listen to me in my site) () Reputation: 100 2004-09-09 12:20:00 Score: 0

DateTime DT1, DT2;

DT1.MONTH-DT2.MONTH

From January 2004, December 2003, a few months? Is the -11?

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 12:24:16 Score: 0

The best way:

(DateTime1.year-datetime2.year) * 12 (DateTime1.month-datetime2.mont);

TOP

Reply to: BIBI5211 () () Reputation: 100 2004-9-9 12:28:04 Score: 0

Agree to the Timespan upstairs

TOP

Reply to: avon520 () reputation: 96 2004-9-9 12:31:50 Score: 0

UP

Concern ...

TOP

Reply to: jinglelin (Bihai Blue Sky) () Reputation: 100 2004-9-9 12:37:19 Score: 0

Triout is very correct

(DateTime1.year-datetime2.year) * 12 (DateTime1.month-datetime2.mont);

TOP

Reply to: Redlotoc (Red Lotus) () Reputation: 100 2004-9-9 12:39:55 Score: 0

UP

TOP

Reply to: The123 (I have to listen to me in my site) () Reputation: 100 2004-9-9 12:41:17 Score: 0

Well, Triout is right

I have a problem in that year.

TOP

Reply to: The123 (I have to listen to me in my site) () Reputation: 100 2004-9-9 12:48:26 Score: 0

The landlord can also try this way

First compare the size of DT1 and DT2

Assume that DT1 is large.

IF ((DT1.Year-dt2.year) == 0)

DT1.MONTH-DT2.MONTH

Else

DT1.MONTH-DT2.MONTH (DT1.Year-dt2.year) * 12

TOP

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-9-9 12:52:26 Score: 0

Thank you for answering my question, but here I ask for the natural month (there are extra days to go), which means that the problem should be considered. (DateTime1.year-datetime2.year) * 12 (DateTime1.month-datetime2.mbon) is not available

But when a consideration, (2-28) - (1-31) must be a month and the like.

Simply put, I want to reach D2 = D1.Addmonth (i), then Submonth (d2, d1) = i's effect, everyone help I think about this subMonth is the implementation

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 13:00:01 Score: 0

I don't understand what you mean.

The method I provide can be solved.

One problem that may exist in the middle is:

DateTime1's Day may be 31, and DateTime2's Day may be 1, so it will be less than one month. So you can further judge on the basis of my algorithm.

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 13:01:59 Score: 0

In fact:

(DateTime1.year-datetime2.year) * 12 (DateTime1.Month-datetime2.mont)

For DateTime1 in September 2004, DateTime2 was January 2003, the answer received was 20 months (1 year, 8 months)

Similarly, if DateTime1.addmonth (20) is obtained, it is also September 2004.

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 13:03:10 Score: 0

So, according to your statement:

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-09-09 12:52:00 Score: 0

Thank you for answering my question, but here I ask for the natural month (there are extra days to go), which means that the problem should be considered.

(DateTime1.year-datetime2.year) * 12 (DateTime1.month-datetime2.mbon) is not available

But when a consideration, (2-28) - (1-31) must be a month and the like.

Simply put, I want to reach D2 = D1.Addmonth (i), then Submonth (d2, d1) = i's effect, everyone help I think about this subMonth is the implementation

I don't know if you have tests.

Of course, there is still a large and small problem with the algorithms I have given, and negative numbers.

TOP

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-9-9 13:10:34 Score: 0

Because D1 and D2 may not be the same, such as (5-21) - (3-16) = 2 (5-2) - (3-17) = 1; Of course, it is also possible to take into account the sky. But there is such a problem, such as (2-28) - (1-31) how much? It is not the same as the month.

TOP

Reply to: Raozr (Lei Chen Yu) () Reputation: 91 2004-9-9 13:19:29 Score: 5

///

/// Same Common Params as the vbscript datediff: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp//// * Sample Code :

/// * system.datetime dt1 = new system.datetime (1974, 12, 16);

/// * system.datetime dt2 = new system.datetime (1973, 12, 16);

/// * page.response.write (CONVERT.TOSTRING (Datediff ("t", DT1, DT2));

/// * * /

///

///

///

///

///

Public Static Double Datediff (String HowTOCompare, System.Datetime STARTDATE, SYSTEM.DATETIME Enddate)

{

Double Diff = 0;

Try

{

System.TimeSpan TS = New System.TimeSpan (StartDate.ticks-Enddate.ticks);

#Region Converstion Options

Switch (Howtocompare.tolower ())

{

Case "M":

DIFF = Convert.TODOUBLE (Ts. TOLMINUTES);

Break;

Case "S":

DIFF = Convert.Todouble (Ts.Totalseconds);

Break;

Case "T":

DIFF = Convert.TODOUBLE (TS.TICKS);

Break;

Case "mm":

DIFF = Convert.Todouble (Ts.TotalmilliseConds);

Break;

Case "YYYY":

DIFF = Convert.Todouble (Ts. TotalDays / 365);

Break;

Case "Q":

DIFF = Convert.TODOUBLE ((ts.totaldays / 365) / 4);

Break;

DEFAULT:

// d

DIFF = Convert.Todouble (Ts. TOLDAYS);

Break;

}

#ndregion

}

Catch (Exception E)

{

DIFF = -1;

Debug.writeline ("Generic Exception Handler: {0}", E.MESSAGE.TOSTRING ());

Throw;

}

Return DIFF;

}

TOP

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-9-9 13:23:46 Score: 0

On the upstairs, there is no month, and there is no consider the situation of the month.

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 13:34:00 Score: 0 Relying on, you want a natural month, how many days are you doing? Regardless of the month, this algorithm is correct, what else is there?

Or, you said that the problem of the month, for the Queen Calend, only the year, how come the month? Do you want to calculate the difference between the two lunar calendar? Then please call another.

TOP

Reply to: hhh63 (hhhh63) () Reputation: 100 2004-9-9 13:37:47 Score: 20

Talk about my opinion, the number of days per month is not necessarily, "D2 = D1.Addmonth (i), then the effect of SubmonTh (D2, D1) = I is impossible, such as January 31 What is it for a month? So Microsoft's Timespan does not have a calculation method of the month.

However, in practice, sometimes the number of months, such as I do childhood ability test software is calculated by the agency, the method of use is

(Y2 - Y1) * 12 (M2 - M1) (D2 - D1) / 30 and surrounded by a single age.

It is impossible to get a precise month, and it is not required to be too accurate in practical applications, and find a similar formula.

TOP

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-9-9 13:40:14 Score: 0

Triout (Stupid) brother still didn't see the premise, "extra days will be reluctant", that is, how many months is the day when I want to know the days during the two days.

TOP

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-9-9 13:42:47 Score: 0

Hhhh63 brother, it seems that there is no ready-made method, it seems that I can only write one yourself.

TOP

Reply to: Triout (Stupid) () Reputation: 90 2004-9-9 14:06:23 Score: 0

Since you are the number of days differences in the two dates, then:

If you don't consider the number of days per month, you can use Timespan's way:

Timespan TS = DateTime1-datetime2;

INT MONTHS = Ts. TOTALDAYS / 30;

Use it if you have to take into account the number of days per month, use:

(DateTime1.year-datetime2.year) * 12 (DateTime1.Month-datetime2.mont)

What is the problem?

Or, you will ask: What should I calculate if the end of the month end? As long as you are simple to judge:

DateTime1's Day DateTime2's Day is a simple problem, as long as:

DateTime1.day-Datetime2.day can be judged that if 0, the above calculation is just, otherwise it will be poor.

The problem must be asked clearly, then yourself have a lot of brains. I want to give you an answer that I have clearly understood, just how you use it as needed.

I have already told 1 1 = 2, I will never ask 1 2, how much is

TOP

Reply to: YZXDC (YZXDC) () Reputation: 93 2004-9-9 14:20:58 Score: 0

I have been mentioned in front of the reply, and the old brother, don't you explain it clear?

Simply put, it is to consider things at the end of the month, such as (1-31) .addmonth (1) = (2-28), so (2-28) - (1-31) must be equal to 1. But 2-28 is the end of the month depending on the month of February, which means that it is not a month (2-28) - (1-31) = 1, what is 0 Top

Reply to: hhh63 (hhhh63) () Reputation: 100 2004-9-9 17:30:59 Score: 0

I don't know if this problem is an actual problem or theoretical research.

If it is a practical problem, it is better to do it, calculate a probably accurate value, such as 5 months for 4.2 months, 4.2 months. But I don't quite agree with Triout's algorithm

(DateTime1.year-datetime2.year) * 12 (DateTime1.Month-datetime2.mont)

On January 31 to February 1st, January 1st, on February 28, it is not a month, it is not very suitable, or add a good day.

If it is theoretical research, it is a histological parade. The current calendar may not add a month and minus a month, and of course the landlord can define some rules themselves.

"Simply, it is to consider things at the end of the month, such as (1-31) .addmonth (1) = (2-28), so (2-28) - (1-31) must be equal to 1. But 2-28 Is it that the end of the month depends on the month of the month, that is, it is not the month (2-28) - (1-31) = 1, what is 0,

List other situations, then induce it, turn into code. However, these rules have a question that everyone is recognized.

TOP

Reply to: ffb (I edited) () Reputation: 92 2004-9-9 17:56:16 Score: 0

AddMonth (-1)

Can get the last month

TOP

Reply to: SexFreebird (Free Bird) () Reputation: 100 2004-9-9 18:16:22 Score: 5

I know that VBScript can easily solve this problem, for example: you have a member for <% = datediff ("D", startdate, enddate)%> Days, "D" indicates that the number of days is returned, and you can use other Characters indicate back to seconds, what to do, such as YYYY, Q quarter, mm month, y, D days, W days, ww weeks, H hours, M minutes, s second.

转载请注明原文地址:https://www.9cbs.com/read-108843.html

New Post(0)