Self-made calendar doesn't count the years right - javascript

i have been busy with a home-made calendar for understanding certain javascript stuff, and i encountered something which for me is very odd.
I have two functions linked to buttons which go to one year before, and one year after the current year.
I also have a function which opens a prompt so you can select any year you want.
The calendar works fine, the buttons work fine, except for one little thing.
Here is the code first:
year = 2014;
function last_year(year,month) {
parseInt(window.year -= 1);
yeartype(window.year,0);
}
function next_year(year,month) {
parseInt(window.year += 1);
yeartype(window.year,0);
}
function chooseYear(year,month) {
window.year = prompt("Which year do you want to see?");
yeartype(window.year,0);
}
yeartype is a function which checks if it's a leap year or not, and then makes the calendar for that specific year.
When i added the function chooseYear, it started to mess things up.
When i enter a year, it loads it. For example, 2020. When i press last_year after i used the chooseYear function, it goes to 2019 (which should happen), but if i press next_year after i used chooseYear, it goes from 2020 to 20201. And if i press last_year after that, it goes back to 20200. For some reason, if i use chooseYear, followed by next_year, it ADDS a number after the existing numbers. Going back a year doesn't give problems though.
It's a really long question, but it's a weird situation to explain. Does anyone know how i canmake it work properly?
Thanks in advance :)

Related

Is the IF function removing the date object in javascript?

I've spent an hour looking for answers and trying different things so I appreciate any help here.
The following code works great for finding someone's part B effective date. However, when someone's birthday is really on the 1st of a month the 'if' function get's used, and I'm no longer able to format and write the date. It's almost like 'partB_eff' is no longer a date object. (I'm a newbie, so I might just be making this part up.)
I'm getting the error "TypeError: partB_eff.toLocaleDateString is not a function at AutoFill_6_Step_Checklist(Code:24:27)"
How can I resolve this?
let birthday = new Date(e.values[2]);
//this is a date entered from a google form
let bdayCopy = new Date(birthday);
//I created this since I'll be using .setMonth(), and I don't want to change the original date of the birhtday
let bday65 = new Date(bdayCopy.setMonth(bdayCopy.getMonth()+780));
//finds the 65th birthday
let partB_eff = new Date(bdayCopy.setDate(01));
//find's the Medicare part B effective date (the 1st of the month someone turns 65)
if(birthday.getDate()==1){
partB_eff = partB_eff.getMonth-1;
//if the person's birthday is really on the 1st of the month, the part b effective date is the 1st of the month prior. partB_eff must be converted
}
partB_eff = partB_eff.toLocaleDateString('en-us',{year:"numeric",month: "short",day:"numeric"});
//format partB_eff so that it looks nice on paper
partB_eff = partB_eff.getMonth-1;
Doesn't do what you think it does. What it does is get the vound function getDate from your date object, and attempt to subtract one from it. In any other language trying to do subtraction on a function would be a type error, but Javascript is Javascript and allows numeric operations on almost any type. A function minus a number in JS is NaN. NaN doesn't have a method called toLocaleString, hence the error.
What's interesting is that you did the same operation correctly above with bdayCopy.setMonth(bdayCopy.getMonth()+780)
Just do the same thing here
bdayCopy = new Date(bdayCopy.setMonth(bdayCopy.getMonth()-1));
Also some important concepts. if in Javascript is not a function. if is a keyword that starts a conditional statement. You can't do any of the things you can do with a function with if. You can't call it or assign it to a variable or pass ot as a function argument. Clearly understanding what a function is is something you need to do to be able to work in JS, or frankly any other language.
Finally if you are doing date math in JS I strongly recommend you use a date library like DateFns or Moment. Javascript native date APIs are possibly the worst designed date API of any language ever.

Disable button until new year

I have the following screen in my applicaton:
I'm trying create a functionality to disable the "Execute" button until the next year arrives when the "Delivered" button is click. At this moment i'm trying to use a Session variable but i'm struggling a bit implementing this functionality correctly.
Can any one advice me of the best way to do something like this?
Thank you
Well, for a front-end solution, you can do something like this. In this case, the button won't be enabled if the year is before 2019:
var year = new Date().getFullYear();
if (year < 2019) {
document.querySelector('#disabled').disabled = true;
}
<button id="disabled">Wait until 2019, please</button>
BUT you will need to verify the date on the server-side too, otherwise everyone could trick your system.

How can I filter by start date in AngularJS?

I have a search feature that should allow a user to sort through a list of courses based on a few factors such as the course language, subject, effort, and whether the course is open for enrollment, opening for enrollment soon, or archived; based on the date property in the course object. I have the other functions working well but the date sort is stumping me.
I'm blaming lack of sleep last night for my inability to wrap my head around this seemingly simple problem.
My addled brain believes that the proper way to begin this is to parse the date and create a filter along the lines of this ...
namespace.filter('dateFilter', function(){
//following is psuedocode
var output[];
if(dateNow > ds.course_open_date) {logic}
if(dateNow > ds.course_open_date + 30 days) {logic}
if(dateNow > ds.course_close_date) {logic}
if(boolean ds.course_self_paced = true) {logic}
return output;
});
I could use a push in the right direction. I need sleep.

Variable checking in JSP

I am fairly new to the JSP scene, but have been programming in other languages for a few years. I am writing an application where I am passing a different variable to java based on a selection option. My main question was where to do the variable checking to see which value to pass?
Here is my current state of value checking (I am currently rewriting it in JSTL as I found sciplets are bad practice).
if (request.getParameter("Freq").equals("Minutes")) {
runOn = request.getParameter("numberOfMinutes");
} else if (request.getParameter("Freq").equals("Weekly")) {
runOn = request.getParameter("dayOfWeek");
} else if (request.getParameter("Freq").equals("Monthly")) {
runOn = request.getParameter("dayOfMonth");
} else {
runOn = "-1";
}
Should this sort of check be done on the jsp page, in javascript, or somewhere else? Looking for a best practice kind of answer.
Side question: Right now, I create a number of different drop down boxes and hide/unhide them using an onChange function in javascript. Would it be better to have one list box where the options change in javascript or keep the current system? Essentially the list boxes are asking the user what day they want their process to run. So if frequency is weekly, a list box with the days of the week would show, but if they have month selected, a list of numbers from 1 to 31 shows. It could all be done with one list box that changes its options when the 'frequency' drop down changes. Again, I'm looking for a best practice answer?
Thank you in advance!

Script to replace an image (.jpg) daily with another image? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a Visual Studio 2005 site which has been up a running for a while. I have an image (.JPG) on this site that I think I need to apply some JavaScript to or something?
Essentially I have 25 images for the month of December and instead of going into the site daily to change this image to another one I was hoping to be able to complete this by code automatically.
I have some .NET skills but I think that I might needs to use JavaScript to complete this task. Can anyone point me in the right direction or provide me with some sample code that could help?
I think the simplest way would be to rename all images to things like "December1.jpg", "December2.jpg", "December3.jpg" etc. Then server-side you would have this:
<img src="/path/to/images/December<%=DateTime.Now.Day%>.jpg"/>
This would just add the day of the month to your image name. There are fancier ways to do it, but for a one-time-deal, you can't get much simpler than this.
EDIT:
You might also throw in an "if exists" for your images so that on the 26th you don't end up with a 404 image. Something like this:
Change your image to add an id and default it to hidden:
<img id="dayImage" runat="server" Visibility="false" src="/path/to/images/December<%=DateTime.Now.Day%>.jpg"/>
Then on the code behind:
if (File.Exists(#"C:\path\to\images\December" + DateTime.Now.Day + ".jpg"))
{
//this will show the image if it exists on the disk
dayImage.Visibility = true;
}
For more on that go here: http://www.dotnetperls.com/file-exists
The way that I would do it, is to use server-side code, but I think it'll be simpler for everyone to show a JavaScript example. While there are several approaches one might take to accomplish what you're asking, one simple way to do this would be to store the urls to all of the image files as strings in an array, as such:
var urlPath = new Array();
urlPath[0] = "Leave Empty"; //Because there will never be a 0th day of any month...
urlPath[1] = "/Images/nameOfPic1.jpg";
urlPath[2] = "/Images/nameOfPic2.jpg";
urlPath[3] = "/Images/nameOfPic3.jpg";
Then cycle through them by grabbing the date:
var myDate = new Date();
Then get the path to the image based off of getDate():
var currentDate = myDate.getDate();
document.getElementById("imgElement").src=urlPath[currentDate];
Then (depending on how many pics you have for a given month) you can assign a new picture based on the numerical date. Of course, it would, using this example, make sense, to have an amount of pictures equal to the maximum days in a month (31) in order to call them as needed. This way will leave out certain pictures on certain months (months with less than 31 days, however). If you desire to simply cycle through them then do exactly as above, but add this instead of the last two statements (this example assumes you always have 25 pictures):
var currentDate = myDate.getDate();
if(currentDate > 25)
{
currentDate -= 25;
document.getElementById("imgElement").src=urlPath[currentDate];
}
else
{
document.getElementById("imgElement").src=urlPath[currentDate];
}
This isn't totally perfect, as the start of each new month will start the picture list over again, and some pics will be seen more than others. I'm not sure if there is a better way to do this or not, but it should get the job done if your clients aren't too picky. Again, though, I, personally, would use server-side code and set an application variable that is global (for everyone) and would handle this directly and remember the AppState variable (is it clear that I use WebMatrix (C#) yet?) regardless of client-side circumstances.
I hope this helps :)

Categories

Resources