Forum Moderators: open

Message Too Old, No Replies

date and first of the month problem

         

matc

11:51 am on Apr 1, 2004 (gmt 0)

10+ Year Member



Hello All,

I have a javascript slide show console which was coded by someone previous to me which I am trying to get my head around. The problem I have is that it seems to hiccup big time on the first day of the month.

The script applys the previous days date to an image name as thats when the images are upload and time-stamped, i.e. the previous day in the folowing format +DD+MM+YY+

This script works for every other day apart from the first day of each month. I would really appricate some help with solving this.

DTnow = new Date();// create new date object 'DTnow'
Year = DTnow.getYear();// years since 1900 (maybe! - see below)
Month = DTnow.getMonth()+1;// month-of-year field (0-11)
DayM = DTnow.getDate();// day-of-month field (1-31)
Hour = DTnow.getHours(); // hours field (0-23)
Mins = DTnow.getMinutes(); // minutes field (0-59)
Secs = DTnow.getSeconds(); // seconds field (0-59)

if (Month == 0) var monthstr = "Jan";
if (Month == 1) var monthstr = "Feb";
if (Month == 2) var monthstr = "Mar";
if (Month == 3) var monthstr = "Apr";
if (Month == 4) var monthstr = "May";
if (Month == 5) var monthstr = "Jun";
if (Month == 6) var monthstr = "Jul";
if (Month == 7) var monthstr = "Aug";
if (Month == 8) var monthstr = "Sep";
if (Month == 9) var monthstr = "Oct";
if (Month == 10) var monthstr = "Nov";
if (Month == 11) var monthstr = "Dec";

// each month has the number of days as the previous month so
// if it's the first the image will be the last day of the previous month.
if (monthstr == "Jan") var monthdays = "31";
if (monthstr == "Feb") var monthdays = "31";
if (monthstr == "Mar") var monthdays = "28";
if (monthstr == "Apr") var monthdays = "31";
if (monthstr == "May") var monthdays = "30";
if (monthstr == "Jun") var monthdays = "31";
if (monthstr == "Jul") var monthdays = "30";
if (monthstr == "Aug") var monthdays = "31";
if (monthstr == "Sep") var monthdays = "31";
if (monthstr == "Oct") var monthdays = "30";
if (monthstr == "Nov") var monthdays = "31";
if (monthstr == "Dec") var monthdays = "30";

if (Year < 1000){// workaround for incompatible year
Year = Year + 1900;// return values in some browsers where
}// getYear() returns the year *itself*

YY = Year.toString().substr(2,2);// in case dd.mm.YY wanted

DD = DayM-1;
if (DD < 10) {
DD = "0" + DD;// in case DD.mm.yy wanted

}

MM = Month;
if (MM < 10) {
MM = "0" + MM;// in case dd.MM.yy wanted
}

if (DayM == 1 && monthstr == "Jan")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Feb")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Mar")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Apr")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "May")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Jun")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Jul")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Aug")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Sep")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Oct")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Nov")
{
DD = monthdays;
MM = "0" + Month;
}
if (DayM == 1 && monthstr == "Dec")
{
DD = monthdays;
MM = Month;
}

YY = Year.toString().substr(2,2);// in case dd.mm.YY wanted

if (Hour >= 12){
AmPm = "pm";
} else {
AmPm = "am";
}
if (Hour > 12) {
Hour = Hour -12;
}
if (Hour == 0) {
Hour = 12;
}
if (Hour < 10 ) {
Hour = "0" + Hour;
}
if (Mins < 10 ) {
Mins = "0" + Mins;
}
if (Secs < 10) {
Secs = "0" + Secs;
}

var interval = 1200;
var random_display = 0;
var timerID = 0;
var imageDir = "/features/areas/sand";

var imageNum = 0;
imageArray = new Array();
imageArray[imageNum++] = new imageItem(imageDir +DD+MM+YY+ "1200t024.jpg");
imageArray[imageNum++] = new imageItem(imageDir +DD+MM+YY+ "1200t048.jpg");
imageArray[imageNum++] = new imageItem(imageDir +DD+MM+YY+ "1200t072.jpg");
imageArray[imageNum++] = new imageItem(imageDir +DD+MM+YY+ "1200t096.jpg");
imageArray[imageNum++] = new imageItem(imageDir +DD+MM+YY+ "1200t120.jpg");

var totalImages = imageArray.length;

function imageItem(image_location) {
this.image_item = new Image();
this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) {
return(imageObj.image_item.src)
}

function randNum(x, y) {
var range = y - x + 1;
return Math.floor(Math.random() * range) + x;
}

function getNextImage() {
if (random_display) {
imageNum = randNum(0, totalImages-1);
}
else {
imageNum = (imageNum+1) % totalImages;
}

var new_image = get_ImageItemLocation(imageArray[imageNum]);
return(new_image);
}

function getPrevImage() {
imageNum = (imageNum-1) % totalImages;
if(imageNum<0){imageNum=totalImages-1};
var new_image = get_ImageItemLocation(imageArray[imageNum]);
return(new_image);
}

function prevImage(place) {
var new_image = getPrevImage();
document[place].src = new_image;
}

function switchImage(place) {
var new_image = getNextImage();
document[place].src = new_image;
var recur_call = "switchImage('"+place+"')";
timerID = setTimeout(recur_call, interval);
if (document[place].src == "http://www.domain.com/features/areas/sand" +DD+MM+YY+ "1200t120.jpg")
{
interval = 8000;
}
if (document[place].src == "http://www.domain.com/features/areas/sand" +DD+MM+YY+ "1200t096.jpg")
{
interval = 1200;
}
if (document[place].src == "http://www.domain.com/features/areas/sand" +DD+MM+YY+ "1200t072.jpg")
{
interval = 1200;
}
if (document[place].src == "http://www.domain.com/features/areas/sand" +DD+MM+YY+ "1200t048.jpg")
{
interval = 1200;
}
if (document[place].src == "http://www.domain.com/features/areas/sand" +DD+MM+YY+ "1200t024.jpg")
{
interval = 1200;
}
}

var image1 = "/features/areas/sand" +DD+MM+YY+ "1200t024.jpg";
var image2 = "/features/areas/sand" +DD+MM+YY+ "1200t048.jpg";
var image3 = "/features/areas/sand" +DD+MM+YY+ "1200t072.jpg";
var image4 = "/features/areas/sand" +DD+MM+YY+ "1200t096.jpg";
var image5 = "/features/areas/sand" +DD+MM+YY+ "1200t120.jpg";

function getIm1() {
document.slideImg.src = image1;
}

function getIm2() {
document.slideImg.src = image2;
}

function getIm3() {
document.slideImg.src = image3;
}

function getIm4() {
document.slideImg.src = image4;
}

function getIm5() {
document.slideImg.src = image5;
}

thanks, ject

ajkimoto

2:46 pm on Apr 1, 2004 (gmt 0)

10+ Year Member



matc,

How about this to get you your DD, MM, and YY:

<script type="text/javascript">
<!--
var today = new Date()
//determines yesterday as the current datetime minus 24 hours worth of milliseconds
var yesterday=new Date(today - 86400000)
var DD=yesterday.getDate()
var MM=yesterday.getMonth()+1
var YY=yesterday.getFullYear()
alert('yesterday was '+MM+'/'+DD+'/'+YY)
//-->
</script>

ajkimoto