i use datejs and i want to get programme three buttons,
Today : generate two dates limites of this week, the monday and the sunday of thise week
Next : generate two dates limites of the next week
Prev : generate two dates limites of the prev week
here my code
var currentDay = 0;
(currentDay).days().fromNow().next().saturday().toString("yyyy-M-d");
(currentDay).days().fromNow().prev().monday().toString("yyyy-M-d");
the three buttons do
currentDay + 7; currentDay - 7; currentDay = 0;
the probléme is
we are monday 22, and this function return me the monday 15;
The following sample .getWeekRange() function accepts a Date object (or defaults to 'today'), will figure out the Monday of that week, then returns an object with a start and end property for the week.
Example
var getWeekRange = function (date) {
var date = date || Date.today(),
start = date.is().monday() ? date : date.last().monday(),
end = start.clone().next().sunday();
return {
start : start,
end : end
};
};
You can then use the function to acquire the week range for any given Date:
Example
var range = getWeekRange();
console.log("Start", range.start);
console.log("End", range.end);
To get the previous week, just pass in a Date object from the previous week:
Example
var prev = getWeekRange(Date.today().last().week());
To get the next week, just pass in a Date object from the next week:
Example
var next = getWeekRange(Date.today().next().week());
Hope this helps.
I've written some code for this sometime ago:
Date.prototype.getMonday=function(){return this.getDay()==1 ? this.clone().clearTime() : this.clone().prev().monday().clearTime();};
// This function returns the Monday of current week
var today=new Date();
today.getMonday().toString();
today.getMonday().next().sunday().toString();
// start and end of this week
today.getMonday().prev().monday().toString();
today.getMonday().prev().day().toString();
// previous week
today.getMonday().next().monday().toString();
today().getMonday().next().sunday().sunday().toString();
// next week
May these help.
Related
I have this data:
const start = "29.09.2021";
const end = "29.10.2021";
const interval = "days"; //also: "month", "week", "year"
const intervalCount = 3;
how to get an array of dates that exists between start and end with intervals: eq. if interval == "days" and intervalCount == 3 then another dates should be 02.10, 05.10, 08.10, 11.10 etc., but interval can be also "month", "week", and "year" so I must calculate based on the dynamic arguments
I have no idea how to even start, thanks for any help!
This can be done using Date objects found here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You can create a function such as
function getDates(startDateString, stopDateString, interval){
var dates = [];
var start = new Date(startDateString);
var stop = new Date(stopDateString);
while (start < stop) {
dates.push (start.toJSON());
start.setDate(start.getDate() + interval);
}
return dates;
}
And then you can run the function by making this call
dateArray = getDates("09-29-2021", "10-29-2021", 1)
It covers single day iterations. A week is just a 7 day interval.
dateArray = getDates("09-29-2021", "10-29-2021", 7)
Moving months gets into edge cases that can be handled through conditions based on your use case (For example, if the date is Jan 31st, should a jump of 1 month take you to Mar 3rd or Feb 28th/29th?)
I am looking to do something quite complex and I've been using moment.js or countdown.js to try and solve this, but I think my requirements are too complex? I may be wrong. Here is the criteria...
I need to be able to have the following achievable without having to change the dates manually each year, only add it once and have many countdowns on one page.
Find current date
Find current year
Find current month
Find day within week of month that applies
¬ 3rd Sunday or 2nd Saturday
Convert to JS and output as html and run countdown
When past date - reset for following year
Pretty mental. So for example if an event is always on the 3rd Sunday of March. The date would not be the same each year.
2016 - Sunday March 19th
2017 - Sunday March 20th
2018 - Sunday March 18th etc.
I hope this is explained well, I realise it may be a total mess though. I managed to get it resetting each year with the date added manually but then someone threw in the spanner of the date being different each year.
var event = new Date();
event = new Date(event.getFullYear() + 1, 3 - 1, 19);
jQuery('#dateEvent').countdown({ until: event });
<div id="dateEvent"></div>
I have edited this answer as I have now put together a solution that works for me. As I believe this isn't simple coding due to the fact it wasn't actually answered 'Please, this is basic coding. pick up a javascript book and learn to code', yeah thanks...
// get the specific day of the week in the month in the year
function getDay(month) {
// Convert date to moment (month 0-11)
var myMonth = moment("April", "MMMM");
// Get first Sunday of the first week of the month
var getDay = myMonth.weekday(0); // sunday is 0
var nWeeks = 3; // 0 is 1st week
// Check if first Sunday is in the given month
if (getDay.month() != month) {
nWeeks++;
}
// Return 3rd Sunday of the month formatted (custom format)
return getDay.add(nWeeks, 'weeks').format("Y-MM-D h:mm:ss");
}
// print out the date as HTML and wrap in span
document.getElementById("day").innerHTML = '<span>' + getDay() + '</span>';
Using
<script src="moment.js"></script>
Hope it helps someone - I'll update when I figure how to + 1 year after it's checked current date and event has passed. I'll look in that JS book.
Please take a look at the below code, I explained in the comment what what does.
You use it by supplying a javascript Date object of any wished start date, and then add as a second value the corresponding year you wish to know the date in.
var date = new Date("2016-03-20");
function getDayInYear(startDate, year) {
// get a moment instance of the start date
var start = moment(startDate);
// collect the moment.js values for the day and month
var day = start.day();
var month = start.month();
// calculate which week in the month the date is.
var nthWeekOfMoth = Math.ceil(start.date() / 7);
// Build up the new moment with a date object, passing the requested year, month and week in it
var newMoment = moment(new Date(year,month,(nthWeekOfMoth * 7)));
// Return the next instance of the requested day from the current newMoment date value.
return newMoment.day(day);
}
var oldMoment = moment(date);
var newMoment2017 = getDayInYear(date,2017);
var newMoment2018 = getDayInYear(date,2018);
console.log(oldMoment.format('YYYY MMMM dddd DD'));
console.log(newMoment2017.format('YYYY MMMM dddd DD'));
console.log(newMoment2018.format('YYYY MMMM dddd DD'));
/** working from today up to 10 years into the future **/
var date = new Date();
var year = date.getFullYear();
for(var i = 0; i < 11; i++) {
console.log(getDayInYear(date, year+i).format('YYYY MMMM dddd DD'));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
I have a date range that looks like this
let start = moment(this.absence.FromDate);
let end = moment(this.absence.ToDate);
The user can decide to deactivate specific week days during that date range, so I have booleans
monday = true;
tuesday = false;
...
I want to create a function that allows me to put all mondays during my date range in an array.
I've looked around on stack but I can only find help for people who need all the monday from a month for example.
You can get next Monday using .day(1) and then loop until your date isBefore your end date adding 7 days for each iteration using add
Here a live sample:
//let start = moment(this.absence.FromDate);
//let end = moment(this.absence.ToDate);
// Test values
let start = moment();
let end = moment().add(45 , 'd');
var arr = [];
// Get "next" monday
let tmp = start.clone().day(1);
if( tmp.isAfter(start, 'd') ){
arr.push(tmp.format('YYYY-MM-DD'));
}
while( tmp.isBefore(end) ){
tmp.add(7, 'days');
arr.push(tmp.format('YYYY-MM-DD'));
}
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I am creating a Discord bot with node.js and discord.js, and there's a feature that allows users to vote thanks to a command, but I'd like them to vote only once a week.
The issue is that, on this Discord, weeks start on Friday and end on Thursday, therefore I can't simply write :
var weekNow = moment().week();
var weekLastVote = moment(dateLastVote).week();
if (weekNow == weekLastVote){
//Prevent from voting again
} else {
//Let the user vote
}
Therefore, I have written some code that seems to work, but I'd like your opinion on it as it seems very sloppy and I'm not sure if I have taken into account all of the possibilities (I don't know if I need to use my month variables for example):
module.exports = {
isSameWeek: function (dateLastVote) {
// moments for today's date
var dayNow = moment().weekday();
var weekNow = moment().week();
var monthNow = moment().month();
var yearNow = moment().year();
var dateNow = moment().format('MMDDYYYY'); // moment without hours/minutes/seconds
// moments for last vote's date
var dayLastVote = moment(dateLastVote).weekday();
var weekLastVote = moment(dateLastVote).week();
var monthLastVote = moment(dateLastVote).month();
var yearLastVote = moment(dateLastVote).year();
var dateLastVote = moment(dateLastVote).format('MMDDYYYY'); // moment without hours/minutes/seconds
if ((yearNow === yearLastVote && weekNow === weekLastVote && dayLastVote < 5) || // 5 = Friday, starting day of the week (a week = Friday to thursday)
(yearNow === yearLastVote && weekNow - 1 === weekLastVote && dayLastVote >= 5 && dayNow < 5) ||
(dateNow === dateLastVote)
){
return true;
} else {
return false;
}
}
};
As I said, this seems do to the trick but I would like someone else's opinion on it to be sure there isn't a simpler way or, if there isn't, if I haven't forgotten anything.
Thank you for reading :)
I do not know how our approaches compare to each other in matter of performance, but I still wanna show my approach on the problem:
function isSameWeek(firstDay, secondDay, offset) {
var firstMoment = moment(firstDay);
var secondMoment = moment(secondDay);
var startOfWeek = function (_moment, _offset) {
return _moment.add("days", _moment.weekday() * -1 + (_moment.weekday() >= 7 + _offset ? 7 + _offset : _offset));
}
return startOfWeek(firstMoment, offset).isSame(startOfWeek(secondMoment, offset), "day");
}
What the solution does is calculating the start of the week of each of the given dates in respect to the offset (for values >= -7 and <= 0) and returning whether both have the same start of the week. Same start of the week = same week.
All you have to do is call the function passing two date objects (or moment objects) and an offset between -7 and 0, depending on how the week is shifted in relation to a "regular" week.
I think that the best way to do want you need is to tell moment that your week starts on Friday. You can simply use updateLocale method customizing dow (day of week) key of the week object and then use your first code snippet. See Customize section of the docs to get more info about locale customization.
Here a live example of setting a custom day as first day of the week and then using your code to check if a given day is in the current week:
moment.updateLocale('en', {
week: {
dow : 5, // Friday is the first day of the week.
}
});
function checkWeek(dateLastVote){
var weekNow = moment().week();
var weekLastVote = moment(dateLastVote).week();
if (weekNow == weekLastVote){
//Prevent from voting again
console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is in the current week')
} else {
//Let the user vote
console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is NOT in the current week')
}
}
checkWeek('2017-05-30'); // same week mon-sun, but previous week fri-thu
checkWeek('2017-06-01'); // same week mon-sun, but previous week fri-thu
checkWeek('2017-06-08'); // next week mon-sun, but current week fri-thu
// First day of the current week
console.log(moment().startOf('week').format('YYYY-MM-DD'));
// Last day of the current week
console.log(moment().endOf('week').format('YYYY-MM-DD'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
EDIT An improved solution is to use moment isSame passing 'week' as second parameter. As the docs states:
Check if a moment is the same as another moment.
If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
Here a live sample:
moment.updateLocale('en', {
week: {
dow : 5, // Friday is the first day of the week.
}
});
function isSameWeek(dateLastVote){
var now = moment();
var lastVote = moment(dateLastVote);
if (now.isSame(lastVote, 'week')){
//Prevent from voting again
console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is in the current week')
} else {
//Let the user vote
console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is NOT in the current week')
}
}
isSameWeek('2017-06-10'); // same week mon-sun, but next week fri-thu
isSameWeek('2017-06-03'); // previous week mon-sun, but current week fri-thu
isSameWeek('2017-06-06'); // current week both mon-sun and fri-thu
// First day of the current week
console.log(moment().startOf('week').format('YYYY-MM-DD'));
// Last day of the current week
console.log(moment().endOf('week').format('YYYY-MM-DD'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
According to the Moment docs you can set the ISO start of the week:
moment().isoWeekday(1); // Monday
moment().isoWeekday(7); // Sunday
then you can use the same functionality to check if the days are in the same week of the year.
Take a look:
https://momentjs.com/docs/#/get-set/iso-weekday/
I am trying to create a simple script that gives me the next recycling date based on a biweekly schedule starting on Wed Jul 6, 2011. So I've created this simple function...
function getNextDate(startDate) {
if (today <= startDate) {
return startDate;
}
// calculate the day since the start date.
var totalDays = Math.ceil((today.getTime()-startDate.getTime())/(one_day));
// check to see if this day falls on a recycle day
var bumpDays = totalDays%14; // mod 14 -- pickup up every 14 days...
// pickup is today
if (bumpDays == 0) {
return today;
}
// return the closest day which is in 14 days, less the # of days since the last
// pick up..
var ms = today.getTime() + ((14- bumpDays) * one_day);
return new Date(ms);
}
and can call it like...
var today=new Date();
var one_day=1000*60*60*24; // one day in milliseconds
var nextDate = getNextDate(new Date(2011,06,06));
so far so good... but when I project "today" to 10/27/2011, I get Tuesday 11/8/2011 as the next date instead of Wednesday 11/9/2011... In fact every day from now thru 10/26/2011 projects the correct pick-up... and every date from 10/27/2011 thru 2/28/2012 projects the Tuesday and not the Wednesday. And then every date from 2/29/2012 (leap year) thru 10/24/2012 (hmmm October again) projects the Wednesday correctly. What am I missing? Any help would be greatly appreciated..
V
The easiest way to do this is update the Date object using setDate. As the comments for this answer indicate this isn't officially part of the spec, but it is supported on all major browsers.
You should NEVER update a different Date object than the one you did the original getDate call on.
Sample implementation:
var incrementDate = function (date, amount) {
var tmpDate = new Date(date);
tmpDate.setDate(tmpDate.getDate() + amount)
return tmpDate;
};
If you're trying to increment a date, please use this function. It will accept both positive and negative values. It also guarantees that the used date objects isn't changed. This should prevent any error which can occur if you don't expect the update to change the value of the object.
Incorrect usage:
var startDate = new Date('2013-11-01T11:00:00');
var a = new Date();
a.setDate(startDate.getDate() + 14)
This will update the "date" value for startDate with 14 days based on the value of a. Because the value of a is not the same is the previously defined startDate it's possible to get a wrong value.
Expanding on Exellian's answer, if you want to calculate any period in the future (in my case, for the next pay date), you can do a simple loop:
var today = new Date();
var basePayDate = new Date(2012, 9, 23, 0, 0, 0, 0);
while (basePayDate < today) {
basePayDate.setDate(basePayDate.getDate()+14);
}
var nextPayDate = new Date(basePayDate.getTime());
basePayDate.setDate(nextPayDate.getDate()-14);
document.writeln("<p>Previous pay Date: " + basePayDate.toString());
document.writeln("<p>Current Date: " + today.toString());
document.writeln("<p>Next pay Date: " + nextPayDate.toString());
This won't hit odd problems, assuming the core date services work as expected. I have to admit, I didn't test it out to many years into the future...
Note: I had a similar issue; I wanted to create an array of dates on a weekly basis, ie., start date 10/23/2011 and go for 12 weeks. My code was more or less this:
var myDate = new Date(Date.parse(document.eventForm.startDate.value));
var toDate = new Date(myDate);
var week = 60 * 60 * 24 * 7 * 1000;
var milliseconds = toDate.getTime();
dateArray[0] = myDate.format('m/d/Y');
for (var count = 1; count < numberOccurrences; count++) {
milliseconds += week;
toDate.setTime(milliseconds);
dateArray[count] = toDate.format('m/d/Y');
}
Because I didn't specify the time and I live in the US, my default time was midnight, so when I crossed the daylight savings time border, I moved into the previous day. Yuck. I resolved it by setting my time of day to noon before I did my week calculation.