Javascript-about calculating the date difference.

Source: Internet
Author: User
Tags date1
Background: I used PHP computing to postpone three months from today (three months are not necessarily 90 days, for example, from January 1, November 30 to January 28, February). The java end said they had a good idea about the date of the three months. Doubt: I know the date difference between two time points, but I want to calculate 3... background: I used PHP computing to postpone three months from today (three months are not necessarily 90 days, for example, from January 1, November 30 to January 28, February). The java end said they had a good idea about the date of the three months.

Doubt: I know the date difference between two time points, but I won't count it for three months. My current practice is as follows:

   echo  floor((strtotime('+3 month') - strtotime(date('Y-m-d')))/86400);

Solution: Use PHP and javascript to calculate the real date difference.

Reply content:

Background: I used PHP computing to postpone three months from today (three months are not necessarily 90 days, for example, from January 1, November 30 to January 28, February). The java end said they had a good idea about the date of the three months.

Doubt: I know the date difference between two time points, but I won't count it for three months. My current practice is as follows:

   echo  floor((strtotime('+3 month') - strtotime(date('Y-m-d')))/86400);

Solution: Use PHP and javascript to calculate the real date difference.

Moment (a js time framework)

Document address: http://momentjs.com/#relative-time

jsmoment("20111031", "YYYYMMDD").fromNow(); // 4 years agomoment("20120620", "YYYYMMDD").fromNow(); // 3 years agomoment().startOf('day').fromNow();        // 15 hours agomoment().endOf('day').fromNow();          // in 9 hoursmoment().startOf('hour'

Other usage methods

jsvar date1 = moment('2015-01-01')var date2 = moment('2000-01-01')date1.diff(date2)  // 473385600000date1.from(date2)  // "in 15 years"

CarbonMaybe it's the best wheel for time processing in PHP: http://carbon.nesbot.com/docs/#api-difference

javascript// Implementation method used in the previous project var f = function (date, diff) {var day; if (typeof date = 'string ') {// yyyy-mm-dd is not in the valid format of date = date. replace (/-/g, '/');} date = new Date (date); day = date. getDate (); diff = ~~ Diff | 0; date. setMonth (date. getMonth () + diff); // when the number of days exceeds the maximum number of days in the target month, it will be postponed to the next month // and then set the nth day of the next month, reset to the last day of the target month if (date. getDate ()! = Day) {date. setDate (0);} return date;} f ('2014/1/30', 3 );

Js version,

var d = new Date();d.setMonth(d.getMonth() + 3);

I remember that there is a very useful time function in php, which can be directly added to the month. The php operation time is the easiest, just add 3 months.

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");        Date date=new Date();        String dateString=sdf.format(date);        date.setMonth(date.getMonth()+10);        String dateString1=sdf.format(date);

====== Result ====

2015-06-10  :2016-04-10

In this case, we need to compare the php and java algorithms. in this case, the php side understands the + n month algorithm.

/*** @ Var $ days int total days * @ var $ year int current year * @ var $ month int Current month */$ days = 0; $ year = 2015; $ month = 3;/*** @ var $ n int months */for ($ I = 0; $ I <$ n; $ I ++) {$ _ days = getMonthDaysByYearAndMonth ($ year, $ month); $ days + =$ _ days; $ month ++;} // Then + n month is converted into + n days

But I don't know if java is like this, but the logic analysis shows that the landlord should understand how the js end works.

For example, date ('Y-m-D', strtotime ('2017-01-30 + 1 month') = date ('Y-m-D ', strtotime ('2017-01-30 + 31 days ') is March 2

Is it not easy to say that your code is improved?

echo  floor((strtotime('+3 month') - time())/86400);

If today is July 22, February 28 and java can calculate that today is July 15, May 30, what should we do in July May 28? Not as intelligent as that language.

If you want this result (based on the distance from the current date to the first day of the next month), php can do the same.

Echo date ('Y-m-D', strtotime ('Last day of + 3 month')-(strtotime ('Last day of this month ') -strtotime ('today ')));

Calculate the distance from January 1, 1st day of next month, and then add three months at the end of the current month, minus this distance.
Obviously, this can be achieved in any language.

phpdate('Y-m-d',strtotime('-3 months'));

I don't understand the problem. Is it a delay of three months for "today?

If the delay is three months, the number is:

phpdate('d',strtotime('-3 months'));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.