I have access to financial end date and need to compute the finanacial start date based on that.
This is the logic I have currently.
moment('2017-03-31', 'YYYY-MM-DD').quarter() would give me the quarter which in this case is 1.
I then subtract 3 months using
moment('2017-03-31', 'YYYY-MM-DD').subtract(3, 'months').format('YYYY-MM-DD') which results in 2016-12-31 and then I do all kinds of hacky computations to arrive at 2017-01-01.
Is there an elegant way to compute the start date using moment API's.
So essentially, given
2017-03-31 -> output: 2017-01-01
2017-12-31 -> output: 2017-10-01
let date = '2017-12-31'
moment(date).startOf('quarter').format('YYYY-MM-DD');
If it your goal to subtract 3 months from the end date and then result in a start date (which is the first of the month) maybe use:
moment().startOf('month');
Moment Docs
var d = moment('2017-03-31', 'YYYY-MM-DD').subtract(2, 'months').startOf('month').format('YYYY-MM-DD');
http://jsfiddle.net/rLjQx/57925/
you can use as per below :
var date = moment('2017-03-31').subtract(2, 'month').startOf('month').format('YYYY-MM-DD');
enter code here
Related
I'm trying to obtain the Duration (or start and end dates) of an arbitrary 'quarter' in Luxon.
For example, suppose I want the beginning and ending dates of the 3rd quarter knowing only the quarter:
const quarterInQuestion = 3;
const startDateOfQuarter = DateTime.fromFormat(quarterInQuestion.toString(), 'q');
This will give me the start date of the quarter, but how can I obtain the end date as well. I've looked into Durations and Intervals but can't seem to get anything to work yet.
Many thanks!
I think you want the endOf method, to which you can pass the period that you want the end of from a date.
const startDateOfQuarter = DateTime.fromFormat('3', 'q');
const endDateOfQuarter = startDateOfQuarter.endOf('quarter')
Like the title says:
I'm looking for a way to let a user fill in a form where they have to set a start date and end date.
The end date can be max 2 months after the start date they filled in.
Does anyone knows a way how to do that?
Thanks!
You can use momentJS, a great lib to handle dates and times.
const startDate = '20180104'; // could be another format, check the doc
moment(startDate).add(2, 'months')`
The lib is really powerful, be sure to check it ;)
I just want to ask if you have an idea on how to get the collection of date.
Sample:
Sunday = ['12/03/2017','12/10/2017','12/17/2017','12/24/2017','12/31/2017'....];
It collected all the date that will fall on Sunday.
Is there a function in javascript or php that could help me or should I do it manually?
Thanks for the suggestion
For JAVASCRIPT Use Moment.js
Your question is related to this post
Check if the input day is Monday with moment.js
moment(date).weekday(1)
For PHP Use function date($format, $timestamp) then use format l (lowercase 'L') A full textual representation of the day of the week
if(date('l', $date) == 'Sunday'){};
See full documentation here
In php
I think first select minimum & maximum date from which you want to start your collection.
E.g:
$s = '2017-01-01';
$e='2017-10-01';
Convert this to time
$stime = strtotime($s);
$etime=strtotime($e);
Get first sunday
$sunday = strtotime('next sunday', $stime);
$format = 'Y-m-d';
Initialise array with first Sunday.
$sun=array(date($format, $sunday));
Use while loop to check and make an array of Sunday's dates
while($sunday<$etime){
$sunday=strtotime('next sunday', $sunday);
array_push($sun, date($format, $sunday));
}
Print_r($sun);
Hope it'll help you out.
Another option with php is to use 2 datetimes and loop though all the sundays between them. You can use modify to set your range and format to specify the format of the date.
For example:
$dayStart = new DateTime('12/03/2017');
$dayEnd = new DateTime();
$dayEnd->modify("+1 month");
$sundays = [];
for ($dayCurrent = $dayStart; $dayCurrent <=$dayEnd; $dayCurrent->modify('next sunday')) {
array_push($sundays, $dayCurrent->format('m/d/Y'));
}
Demo
I am creating a platform for recurring monthly orders.
I am using later.js for the recurrence. I have come across the following two cases and I am wondering if anybody has suggestions on how to better handle these (or if later.js handles them natively somehow):
later.parse.recur().on(31).dayOfMonth()
The date is the 31st of a given month. Current result is that is jumps months that end on the 30th. WORKAROUND: is to use last().dayOfMonth().
later.parse.recur().on(30).dayOfMonth()
later.parse.recur().on(31).dayOfMonth()
Month of February, ending on the 28th or 29th. How to handle if the date is 30th (or 31st). WORKAROUND: If date > 28th, add .and().on(59).dayOfYear()
Thanks!
I don't know the specifics of later.js, but apparently you can write something called a custom modifier: https://github.com/bunkat/later/blob/master/example/modifier.js
In addition to this, if you add a month to a javascript date (doesn't matter if the number becomes greater than 11/december), set the day of the month to the first then subtract 1 day, then you'll get the date of the last day in the originally given month. For example:
var a = new Date("2000-02-25");
var b = new Date(new Date(a.getFullYear(),a.getMonth()+1,1)-1);
console.log(b);
I need to calculate the actual number of days between a number of date ranges.
eg:
2014-01-01 to 2014-01-04 is 4 days
2014-01-05 to 2014-01-06 is 2 days
while
2014-02-01 to 2014-02-03 3 days
with
2014-02-03 to 2014-02-05 3 days
is a total of 5 days
an added complication is that during a month there will be some gaps between date ranges and or overlapping date ranges to be taken into consideration
any ideas guys.
ability to do the calc using mysql would be great.
Maybe i should have said count the number of days instead of calculate.
I can get the number of days between two date ranges using either mysql or javascript as mentioned below, I think my wheels are coming off with the overlapping date ranges where one range starts before another has finished.
As suggested HERE:
You can use Date objects in Javascript:
var prevTime = new Date(2011,1,1,0,0); // Feb 1, 2011
var thisTime = new Date(); // now
var diff = thisTime.getTime() - prevTime.getTime(); // now - Feb 1
alert(diff / (1000*60*60*24)); // positive number of days
EDIT: I missed you tagged JavaScript, but asked for MySQL
As suggested HERE:
If you are using DATE or DATETIME formatting for your column, you can use:
SELECT DATEDIFF(STR_TO_DATE('2014-01-01', '%Y-%m-%d'),STR_TO_DATE('2014-01-04', '%Y-%m-%d')) AS DAYS
Hope that helps
EDIT2 Here's a nice way to do it in one statement with some logic:
SELECT (CASE
WHEN Start_date1 <= End_date2 THEN
1+DATEDIFF(End_date2, Start_date1)
WHEN Start_date2 <= End_date1 THEN
1+DATEDIFF(End_date1, Start_date2)
ELSE 0
END) AS DAYS
FROM TABLE
The logic is:
Date1 starts before Date2 ends, Start_date1 >= End_date2
OR
Date2 starts before Date1 ends, Start_date2 >= End_date1
If neither is true, they don't overlap.
This little snippet of SQL request code may get you started. It uses DATEDIFF():
SELECT 1+DATEDIFF(MAX(end_date), MIN(start_date)) AS duration_in_days, event_id
FROM event_table
GROUP BY event_id
What's going on here?
First, you've said that the range 21-Aug-14 to 22-Aug-14 is 2 days, but DATEDIFF computes it as 1. So we need to add 1 to it.
Second, the GROUP BY here will aggregate multiple date ranges, if any, for the same event. I have no idea if you're using events; you didn't say. The point here is to show how to aggregate these dates.
Third, if your individual date ranges are non-overlapping, this query won't work correctly. For example, suppose you have these two date ranges
21-Aug-14 to 22-Aug-14
27-Aug-14 to 28-Aug-14
This query will come up with the aggregate range 21-Aug-14 to 28-Aug-14, which is eight days. But you may want to omit the gap 23-Aug to 26-Aug, and only report 4 days. This query won't do that.