Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement to get these dates below in javascript.

I have a drop down with values
Last 15 days
Last month
Last 3 months
Last 6 months
Last 12 months 


Now consider if Today's date is Oct 2 2019 and
if i select
Last 15 days

date range i should get is
(Sept 15 '19 - Sept 30 '19)

if i select
Last month

date range i should get is
(Sept 1 '19 - Sept 30 '19 )

if i select
Last 3 months

date range i should get is
(July 1 '19 - Sept 30 '19)

if i select
Last 3 months

date range i should get is
(April 1 '19 - Sept 30 '19)

if i select
Last 12 months

date range i should get is
( Sept 1 '18 - Sept 30 '19)



Now consider if Today's date is Oct 20 2019

if i select
Last 15 days

date range i should get is
(October 1 '19 - Oct 15 '19)

if i select
Last month

date range i should get is
(Sept 1 '19 - Sept 30 '19)

if i select
Last 3 months

date range i should get is
(July 1 '19 - Sept 30 '19)

if i select
Last 3 months

date range i should get is
(April 1 '19 - Sept 30 '19)

if i select
Last 12 months

date range i should get is
(Sept 1 '18 - Sept 30 '19)


Time range can be of database format.

What I have tried:

i am checking with moment js Managing Dates and Times Using Moment.js[^] and i am working more on below. If any body can help me to get this work really appreciated. Thank you. I will update here with my outcomes.
Posted
Updated 17-Dec-19 2:12am
Comments
ZurdoDev 16-Dec-19 8:04am    
There are tons of examples online. Where are you stuck?
Member 8478234 16-Dec-19 12:53pm    
can you provide me any link. Thank you.

Quote:
...
if Today's date is Oct 2 2019 and
if i select "Last 15 days" date range i should get is (Sept 15 '19 - Sept 30 '19)
...
if Today's date is Oct 20 2019
if i select "Last 15 days" date range i should get is (October 1 '19 - Oct 15 '19)


Seems your issue is related to the algorithm which returns proper end-date of range depending on current date.
The answer is pretty obvious: you have to check if current day is less or greatest than 15. In pseudo code:

JavaScript
if (moment.date() < 15)
{
    enddate = moment.date(0); //last day of previous month
    begdate = enddate.subtract(15, 'days'); //or enddate.add(-15, 'days');
}
else
{
    enddate = moment.date(15); //15. day of current month
    begdate = moment.date(1); //1. day of current month
}


Now, you know how to get last 15 days. The rest belongs to you.

For further details, please see:
Moment.js | Docs[^]
javascript - MomentJS - How to get last day of previous month from date? - Stack Overflow[^]
 
Share this answer
 
v2
let startDate = '', endDate = '';

//date range from 15th to end of previous month
const date = moment().subtract(1,'months').startOf('month').format('MM/DD/YYYY');
console.log('format - startDate', date)
startDate = moment(date, "MM/DD/YYYY").add(14, 'days');
console.log('start date', formatDateforDatePicker(startDate))
endDate = moment().subtract(1,'months').endOf('month').format('MM/DD/YYYY');
console.log('end date', endDate)

//date range from start to 15th of same month
startDate = moment().startOf('month');
console.log('format - startDate', formatDateforDatePicker(startDate))
endDate = moment(startDate, "MM/DD/YYYY").add(14, 'days');
console.log('endDate', formatDateforDatePicker(endDate))
 
Share this answer
 
Comments
Maciej Los 17-Dec-19 8:16am    
Is this an answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900