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

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 :)

Related

date and time picker with validation [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I've been looking around for a decent jQuery plugin that can handle both dates and times. The core UI DatePicker is great, but unfortunately I need to be able to take time in as well.
I've found a few hacks for the DatePicker to work with times, but they all seem pretty inelegant and Google isn't turning up anything nice.
Is there a good jQuery plugin for selecting dates and times in a single UI control with a usable interface?
By far the nicest and simplest DateTime picker option is http://trentrichardson.com/examples/timepicker/.
It is an extension of the jQuery UI Datepicker so it will support the same themes as well it works very much the same way, similar syntax, etc. This should be packaged with the jQuery UI imo.
#David, thanks for the recommendation! #fluid_chelsea, I've just released Any+Time(TM) version 3.x which uses jQuery instead of Prototype and has a much-improved interface, so I hope it now meets your needs:
http://www.ama3.com/anytime/
Any problems, please let me know via the comment link on my website!
In my view, dates and times should be handled as two separate input boxes for it to be most usable and efficient for the user to input. Let the user input one thing at a time is a good principle, imho.
I use the core UI DatePicker, and the following time picker.
This one is inspired by the one Google Calendar uses:
jQuery timePicker:
examples: http://labs.perifer.se/timedatepicker/
project on github: https://github.com/perifer/timePicker
I found it to be the best among all of the alternatives. User can input fast, it looks clean, is simple, and allows user to input specific times down to the minute.
PS:
In my view: sliders (used by some alternative time pickers) take too many clicks and require mouse precision from the user (which makes input slower).
My best experience with a datepicker is with the prototype-based AnyTime. I know that's not jQuery, but it may still be worth the compromise for you. I know absolutely no prototype, and it's still easy enough to work with.
One caveat I've found: it is not forward compatible on some browsers. That is, it did not work with a newer version of prototype on Chrome.
Just to add to the info here, The Fluid Project has a nice wiki write-up overviewing a large number of date and/or time pickers here.
I researched this just recently and have yet to find a decent date picker that also includes a decent time picker. What I ended up using was eyecon's awesome DatePicker, with two simple dropdowns for time. I was tempted to use Timepickr.js though, looks like a really nice approach.
I have ran into that same problem. I actually developed my using server side programming, but I did a quick search to try and help you out and found this.
Seems alright, didn't look at the source too much, but seems to be purely JavaScript.
Take look:
http://www.rainforestnet.com/datetimepicker/datetimepicker.htm
Here is the demo page link:
http://www.rainforestnet.com/datetimepicker/datetimepicker-demo.htm
good luck
This is some code I use to have a user select one
datetimepicker, set the datetime, and have the
other datetimepicker add One Minute to that time.
I needed this for a custom medication control....
Anyway, thought it might help someone else since I could
not find the answer any where online...
(at least not a complete answer)
Keep in mind that the 60000 added, adds one minute.
(60 * 1000 milliseconds)
$('.frdtPicker').datetimepicker({
onClose: function(dateText, inst) {
var endDateTextBox = $('.todtPicker');
if (endDateTextBox.val() != '') {
var testStartDate = new Date(dateText);
var testEndDate = new Date(endDateTextBox.val());
if (testStartDate > testEndDate) {
var testStartDate = new Date(dateText).getTime() + 60000;
var testStartDate2 = new Date(testStartDate);
endDateTextBox.datetimepicker('setDate', (new Date(testStartDate2)));
}
}
else {
var testStartDate = new Date(dateText).getTime() + 60000;
var testStartDate2 = new Date(testStartDate);
endDateTextBox.datetimepicker('setDate', (new Date(testStartDate2)));
}
$('.frdtPicker').val(dateText); //endDateTextBox.val());
},
onSelect: function(selectedDateTime) {
var start = $(this).datetimepicker('getDate');
$('.todtPicker').datetimepicker('option', 'minDate', new Date(start.getTime()));
}
});
Take a look at the following JavaScript plugin.
Javascript Calendar with date and time
I've made it to be simple as possible. but it still in its early days.
Let me know the feedback so I could improve it.
Not jQuery, but it works well for a calendar with time: JavaScript Date Time Picker.
I just bound the click event to pop it up:
$(".arrival-date").click(function() {
NewCssCal($(this).attr('id'), 'mmddyyyy', 'dropdown', true, 12);
});
I make one function like this:
function getTime()
{
var date_obj = new Date();
var date_obj_hours = date_obj.getHours();
var date_obj_mins = date_obj.getMinutes();
var date_obj_second = date_obj.getSeconds();
var date_obj_time = "'"+date_obj_hours+":"+date_obj_mins+":"+date_obj_second+"'";
return date_obj_time;
}
Then I use the jQuery UI datepicker like this:
$("#selector").datepicker( "option", "dateFormat", "yy-mm-dd "+getTime()+"" );
So, I get the value like this: 2010-10-31 12:41:57
We had trouble finding one that worked the way we wanted it to so I wrote one. I maintain the source and fix bugs as they arise plus provide free support.
http://www.yart.com.au/Resources/Programming/ASP-NET-JQuery-Date-Time-Control.aspx

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.

What is the scope of moment.tz.setDefault() when used in Meteor?

TL;DR What is the scope of moment.tz.setDefault()?
I'm sure my problem here stems from my inexperience with both JavaScript and Meteor but I've been struggling with the problem for several straight days now.
I'm working on an app that must take into account the client's timezone but I'm having significant difficulty in forcing the server code to use the client's timezone. Somewhere along the way--that being from the moment the client presses "Submit" to the moment Meteor inserts--my timezone setting is getting lost and local time (of the server) is being used.
The app flow is like this:
(client) user submits form
(client) validation of data is performed
(server) Meteor method is called
(server) validation of data is performed (same code as earlier)
(server) business logic is applied
(server) insert into DB
I capture the timezone at step 1 and try to pass it along through all the steps but I must be missing something because between 4 and 5 the timezone is (seemingly) lost. The fast is, I'm not seeing why. I've checked this 100 times and tried all manner of different permutations but can't figure out where the gap is (I've used soooo many console.log()s it's crazy.)
So instead of trying to set the timezone at every point Moment() is used (because it defaults to calculating in local time) I discovered moment.tz.setDefault() and tried using that at least once on each .js file in my app. But it didn't work.
Reading this it might sound like I'm not doing enough testing but that is not the case. I have spent 10s of hours on this and I'm just not getting it. I'd love to share the code but I think it's just too long and complicated to properly share so I've done my best to explain the problem.
Good news! You're overcomplicating it :-)
Open up a browser console & type time = new Date(). Notice how it's in the correct timezone? That's because the timezone conversion is happening on the client.
Now, type time.valueOf(). As you probably know, you've got the number of milliseconds since 1-1-1970...but in what timezone?? You guessed it, UTC!
So if all you're doing is saving a number, and the client is fully capable of converting that number into the local timezone, why not save the time in UTC on the server? You'll get an ISODate() in your database (which is a fancy int64). Then, when you retrieve it on the client, you can put it in their local time (they might be traveling!) or any other timezone you chose. If it's a meetup in a certain city, simply grab the timezone of that city & apply it to the field. Hint: THIS is the appropriate time to use moment.js, not before!
Edit for time patterns:
Based on the new info, I imagine you have something that accepts an arrivalTime & then makes sure the time is between an earlyArrival and lateArrival say, 7:00 - 8:30AM. So, save the times as dates
timeToDate = function(time) {
return new Date('1970 1 1 ' + time);
};
earlyArrival = timeToDate('7:30 AM');
arrivalTime = timeToDate('8:00 AM');
lateArrival = timeToDate('8:30 AM');
Then, validate via simple math: earlyArrival < arrivalTime.
OR, if you use simple schema (which you should), a validation pattern might look like this:
departureTime: {
type: Date,
min: timeToDate('5:00 PM'),
max: timeToDate('6:30 PM'),
autoValue: function() {
return timeToDate(this.value);
},
custom: function () {
if (this.value < this.field('arrivalTime').value) {
return "lateAfterEarly";
}
}

Self-made calendar doesn't count the years right

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 :)

Javascript declaration issue

//PROBLEM SOLVED//
It turned out that the JQuery Ajax call couldn't reach the URL in certain browsers.
Thanks anyway guys, for your quick responses, definitly helped to work it out.
Sorry for the non-specific title, I don't even think what should be the problem.
There is a JQuery plugin (http://keith-wood.name/countdown.html) which counts down from a specific date or time.
The end time from which the counter should start can be defined in 2 ways: either setting a date either setting the number of seconds left.
My project needs the second one and based on the documentation this option has to be declared like:
$('#digital_hour').countdown({until: +300});
Notice the "+" sign before the number.
It works nice on any OS and device, UNTIL I replace the number 300 with a variable that stores the seconds left until the end of the day on the server. So this version:
$('#digital_hour').countdown({until: +seconds_left_on_server});
works on specific browsers, but on others don't. Strangly enought it works under my Vista/Mozilla20.0 combo, but it doesn't on my Vista/IE6, nor on my friends Ubuntu/Mozilla combo.
I'm not a huge javascript admirer, nor an expert on the subject, but I feel that there is something around the "+" sign.
Can anyone help?
You can try with
$('#digital_hour').countdown({until: new Date(+(new Date()) + 1000 * seconds_left_on_server)});
Have you tried something simple like var seconds_left = 300 and then $('#digital_hour').countdown({until: +seconds_left}); and see what happens?
It sounds like your variable is not storing what it should. The "+" shouldn't be a problem.

Categories

Resources