I am building a function where I would generate dates dynamically depending on what the user sends as a parameter to it. I need to create dates for cases like last week, this week, last month, this month, last 7 days, last 30 days. Since I am sending from and to dates to DB I need to format them like yyyy-mm-dd
. My issue is how to get a date for end of last week and beginning of last week, this week beginning, and correct dates for cases when I deduct 7 or 30 from current date.
This is my code so far, but it is only working for dates in the current month:
function myDate(day){
var date = new Date();
var dd = date.getDate() - day;
var mm = date.getMonth()+1; //January is 0!
var yyyy = date.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var newDate = yyyy+'-'+mm+'-'+dd;
return newDate;
}
var today = myDate(0);
var yesterday = myDate(1);
var sevenDaysAgo = myDate(7);
var thirtyDaysAgo = myDate(30);
Updated code:
This is how I did it using moment.js if anyone needs it:
var date = moment().format("YYYY-MM-DD");
var yesterday = moment().subtract(1, 'day').format('YYYY-MM-DD');
var sevenDaysAgo = moment().subtract(7, 'day').format('YYYY-MM-DD');
var thirtyDaysAgo = moment().subtract(30, 'day').format('YYYY-MM-DD');
var lastWeekStart = moment(date).weekday(-6).format('YYYY-MM-DD');
var lastWeekEnd = moment(date).weekday(1).format('YYYY-MM-DD');
var thisWeekStart = moment(date).weekday(1).format('YYYY-MM-DD');
var startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
var startOfLastMonth = moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD');
var endOfLastMonth = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD');
Date
manipulationFor the curious or library-averse, here are some examples showing how to create your own functions in vanilla JavaScript that do these calculations for you:
/* Define new prototype methods on Date object. */
// Returns Date as a String in YYYY-MM-DD format.
Date.prototype.toISODateString = function () {
return this.toISOString().substr(0,10);
};
// Returns new Date object offset `n` days from current Date object.
Date.prototype.toDateFromDays = function (n) {
n = parseInt(n) || 0;
var newDate = new Date(this.getTime());
newDate.setDate(this.getDate() + n);
return newDate;
};
// Returns new Date object from start of week of current Date object
// optionally offset `n` weeks from week of current Date object.
Date.prototype.toStartOfWeek = function (n) {
var newDate = new Date(this.getTime());
newDate.setDate(this.getDate() - this.getDay());
return n ? newDate.toDateFromDays(n * 7) : newDate;
};
// Returns new Date object from start of month of current Date object
// optionally offset `n` months from month of current Date object.
Date.prototype.toStartOfMonth = function (n) {
n = parseInt(n) || 0;
var newDate = new Date(this.getTime());
newDate.setMonth(this.getMonth() + n, 1);
return newDate;
};
/* Instantiate a Date. */
var today = new Date();
/* Chain all the things. */
console.log(
'Today: ', today.toISODateString()
);
console.log(
'7 days ago: ', today.toDateFromDays(-7)
.toISODateString()
);
console.log(
'30 days ago: ', today.toDateFromDays(-30)
.toISODateString()
);
console.log(
'90 days from now: ', today.toDateFromDays(90)
.toISODateString()
);
console.log(
'Start of this week: ', today.toStartOfWeek()
.toISODateString()
);
console.log(
'End of this week: ', today.toStartOfWeek(1)
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of last week: ', today.toStartOfWeek(-1)
.toISODateString()
);
console.log(
'End of last week: ', today.toStartOfWeek()
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of next week: ', today.toStartOfWeek(1)
.toISODateString()
);
console.log(
'End of next week: ', today.toStartOfWeek(2)
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of this month: ', today.toStartOfMonth()
.toISODateString()
);
console.log(
'End of this month: ', today.toStartOfMonth(1)
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of last month: ', today.toStartOfMonth(-1)
.toISODateString()
);
console.log(
'End of last month: ', today.toStartOfMonth()
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of next month: ', today.toStartOfMonth(1)
.toISODateString()
);
console.log(
'End of next month: ', today.toStartOfMonth(2)
.toDateFromDays(-1)
.toISODateString()
);
See the
MDN documentation for the Date
constructor function for more information on the native methods used in this example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With