Given today is October 2015, in a calendar view, the September 27 - 29 appear on the calendar and Oct 31 appear. I'd like to get the start date that is appearing on the calendar AND the end date, so in this case, it'd be 9/27/2015 and 10/31/2015.
I'm using moment.js to try and calculate this. My thought is I'd get the calendar week and then convert that to find the start date and then get the end week the calendar is showing and convert that to get the last date shown. Its proving to be quite challenging which makes me thinking there is an easier way.
I'm looking at the documentation found here: http://momentjs.com/docs/#/get-set/ but I'm having trouble chaining some of the events together.
I have this to get me the month of the year:
var day = '2015-10-19';
var getStartWeekYear = moment(day).startOf("month").week();
var getEndDateWeekYear = moment(day).endOf("month").week();
and this is returning the expected value of 40 and 44 for the given date (day) but what would the next step be?
This is easier than you think. :)
var day = '2015-10-19';
var start = moment(day).startOf('month').startOf('week').format('YYYY-MM-DD');
var end = moment(day).endOf('month').endOf('week').format('YYYY-MM-DD');
console.log(start); // "2015-09-27"
console.log(end); // "2015-10-31"
Related
I'm developing a website where registrations for a particular event will open on a certain date (say, January 1, 2019) and will close on another date (say, January 10, 2019). I'm using JavaScript to redirect users to the relevant pages if they try to access it on before the 1st or after the 10 of January.
My code so far:
var d = new Date();
var startDate = new Date(2019, 0, 1, 8);
var endDate = new Date(2019, 0, 10, 23, 59);
if(endDate-d<0) // Past expiration date
window.location = "register-closed.html";
else if(startDate-d > 0) // Before starting
window.location = "register-unavailable.html";
The main problem as you might have guessed is that this code takes the local date and time from the user; if I set the date on my device as 2nd January, 2019, I'm able to access the actual register page, even though it's May right now.
I feel this would be a common problem for many, but I've been unable to find any solution to this. How do I get the REAL date and time for my country (India) instead of the device time?
TL;DR
How do I get the actual date and time for a country (in my case, INDIA) using JavaScript? If I can't use vanilla JS, is there some other method to do so?
PS: If you have any solutions that can only be bypassed using methods more complicated than changing your device time, I'll readily accept them. This whole website is just for a high school event, so I don't expect any skilled hackers to spend their time on this :)
This code will get the date (as in the 30th), month, and year. This uses the new Date(); variable type. It has several uses, and you can get the output in whatever order using something like new Date(year, month, day, hours, minutes, seconds, milliseconds). That would output something like Wed May 22 2019 10:46:32 GMT-0400 (Eastern Daylight Time).
var todaysDate = new Date();
var date = todaysDate.getDate();
var month = todaysDate.getMonth();
var year = todaysDate.getFullYear();
if(date === 10 || month === 0 || year === 2019){
//January is 0 because counting starts at 0
...
}
https://www.w3schools.com/jsref/jsref_obj_date.asp
you should take a look to this topic as it seems to answer to your problem using only vanilla JS. Hope it helps :)
So I am kinda stuck in figuring out a certain aspect. What I want to do is the following:
Let's say I just have a simple date display, which will show a date such as October 10th, 2017 to an end user. And then there is an option to subtract a certain number of days from said date (an offset of 1, 2, 3, whatever offset is chosen).
What I am looking to do is completely exclude weekend dates from the count - so if today is Monday, October 9th, and an offset of 1 is selected, it goes to Friday the 6th; if an offset of 2 is chosen, it goes to Thursday the 5th; an offset of 3 goes to Wednesday the 4th...
If today was Wednesday, October 11th, an offset of 2 would take you to Monday the 9th, an offset of 4 would go to Thursday the 5th, and so on (completely disregards / skips weekend dates when counting / subtracting which day to land on).
I have so far been able to only find answers for the functionality to calculate the number of working days excluding weekends, and things of that nature (which I already have, using the momentjs-business npm module, but is not exactly what I need).
I did not post code because this is part of a much larger code base, and I feel posting snippets would only add to the confusion, since I believe the question is relatively simply and straightforward; I do not want to over complicate.
All I would like is to not include weekends at all when setting an offset from whichever date is displayed to the user (the date which is displayed to the user is from a database).
I hope this all made sense, and if more info is needed, please let me know. Thanks in advance for anyone that can point me in the right direction!
This will achieve what you want I think. Please note this is terribly inefficient. If your offset is very large it generates a new date every iteration of the loop. With some tinkering it could be optimized
let startDate = new Date('10/10/2017');
let endDate = "", offset = 2;
while(offset > 0){
endDate = new Date(startDate.setDate(startDate.getDate() - 1));
if(endDate.getDay() !== 0 && endDate.getDay() !== 6){
offset--;
}
}
Here is a working Fiddle
You can use moment-business library. It has the subtractWeekDays that:
Subtract week days from the moment, modifying the original moment. Returns the moment.
Your code could be like the following:
var m = moment("October 10th, 2017", "MMMM Do, YYYY");
business.subtractWeekDays(m, 2);
If you don't want to add an external library, have a look at addWeekDays and subtractWeekDays code.
JavaScript date objects have a getDay() method that tells you what day of the week it is. You could use this to figure out which dates are weekends and exclude them.
var date = new Date();
var dayOfWeek = date.getDay();
console.log(dayOfWeek) // 1 for Monday, 2 for Tuesday, etc.
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 am using a calendar plugin, and the plugin has a few callback events allowing you to customise what happens when a user clicks a date etc. One way to set this is up, for me, is the following:
onDayClick: function(e) {
window.location.href = 'http://www.testdomain.com/events/day/' + e.data.date;
}
.date is a date object so, if clicked, for example, this would return:
http://www.testdomain.com/events/day/Thu Jun 2012 2014 2000:00:00 GMT+0100 (BST)
What I need is the desired output of:
http://www.testdomain.com/events/day/2014/07/17/ and having a look at the date object documentation, I thought this would be fairly easy.
Date.prototype.GetCustomFormat = function() {
return this.getFullYear()+'/'+getInTwoDigitFormat(this.getMonth())+'/'+getInTwoDigitFormat(this.getDate());
};
function getInTwoDigitFormat(val) {
return val < 10 ? '0' + val : val;
}
onDayClick: function(e) {
window.location.href = 'http://www.testdomain.com/events/day/' + e.data.date.GetCustomFormat();
}
But, what this brings back, when clicked, is the right year... but the wrong month by 1 and the wrong date by a few days. Weird. So I added some offets and added a UTCMonth...
return this.getFullYear()+'/'+getInTwoDigitFormat(this.getUTCMonth()+1)+'/'+getInTwoDigitFormat(this.getDate());
This seems to work now. However, if I have an event that lands on the 1st... it uses the 1st from the previous month. So, if I click on 1st July, it returns 1st June.
I think I'm grasping it... but there's a few odd results here and there. Can anyone spot where I am going wrong and put me right?
Thanks
This explanation to this is simple: Because you're one hour ahead of UTC, at midnight on the first of July, UTC is still in June! That's why it outputs June 1st from using the UTC month. Using the UTC month and not UTC year and date doesn't make much sense, so instead, just use the regular month:
Date.prototype.GetCustomFormat = function() {
return this.getFullYear()+'/'+getInTwoDigitFormat(this.getMonth()+1)+'/'+getInTwoDigitFormat(this.getDate());
};
var testerDate = new Date(Date.parse("Thu Jun 1 2012 00:00:00 GMT+0100"))
testerDate.GetCustomFormat() //The output of this depends on your time zone: It'll be 2012/06/01 if in or ahead of GMT+0100, but otherwise, it'll be 2012/05/31.
I'm making a JSON call to COSM (now Xively) so it will return data that I can plot with Highchart's highstock chart. See: jsfiddle.net/T7D5U/2/
Currently the start and end date are hard coded like this:
$.getJSON('http://api.xively.com/v2/feeds/4038/datastreams/9.json?start=2013-05-01T00:00:00Z&end=2013-05-19T23:00:00Z&interval=3600?key=dNSiSvXZtR6QBUqbzll4CCgnngGSAKxIQVFSeXBneGpqWT0g', function(data) {
I want the start and end dates to be dynamic. I want the end date and time to be now. If now was May 19, 2013 2:30 PM, it would be formatted like this:
end=2013-05-19T14:30:00Z
And I'd like the start time to be now minus 10 days, this can be rounded to the day. So the start time would look like this:
start=2013-05-09T00:00:00Z
BTW, I'm not familiar with JavaScript (just C).
Also, when I try an put a jsfiddle link in stackoverflow post, I get an error that says "Links to jsfiddle.net must be accompanied by code." I'm confused by this; I don't know what I'm supposed to do.
I will do it that way :
// Set end to current date and time on client
var end = new Date();
// Copy end date and assign to start
var start = new Date(+end);
// Set date of start to 10 days ago
start.setDate(start.getDate() - 10);
alert(start.toISOString());