Subtract A Day To Date Object Using 'myDate - 1'
May 29, 2007
Am i breaking any rules when I loop dates like
// Determine Memorial Day
intFlag = 0;
memDayHol = new Date (currentYear, 4, 31);
while (intFlag == 0) {
if (memDayHol.getDay() == 1) {intFlag =1;}
else {memDayHol = memDayHol - 1;}
}
I can find no docs that one can use the '+' operator directly on a date
object, and it assumes adding/subtracting a day(s). Is this deprecated
code?
View 10 Replies
Nov 10, 2010
I have the following function that will subtract n number of days from todays date. The problem is that it always returns the the wrong calculate date. The month is wrong.
see if you can spot my mistake:
Code:
function returnDate(ndays){
var dayOfTheWeek = now.getDay();
now.setTime(now.getTime() - nDays * 24 * 60 * 60 * 1000);
alert(now); // returns current date
alet(now.getYear() + "/"+now.getMonth()+"/"+now.getDate()) // returns new calculated date
}
View 5 Replies
View Related
Jul 23, 2005
How do check for a date object?
<script type="text/javascript">
var d=new Date()
var arr=new Array("Jun 5",d,"Mar 16")
for(i=0;i<arr.length;i++){
if(arr[i] != Date()) // is string or is not date object
document.write(arr[i] + "<br>")
}
</script>
View 1 Replies
View Related
Jul 20, 2005
According to the Safari browser the world began on "Fri Dec 13 1901 15:45:52 GMT-0500", but I need to be able to get around this limitation.
I am interested in dates from 1500 to 1901, as far as I can determine, there are 14 possible calendar variations.
Year starts on Sun, Mon..... Leap year starts on Sun, Mon..
I can label these early year "types" as a number between 0 and 13.
Let's say 2005 is type 5, and that 1655 is too. (In both years Jan 1 falls on a Saturday)
I'm trying to create a function that will identify the "type" of year.
function getYearType(year){
return Number // number between 0 and 13
}
View 7 Replies
View Related
Jul 9, 2010
How to extend the constructor for the date object of the javasccript so that whenever a call is made to the constructor, I want to perform a particular action? Basically how to define wrappers for default javascript methods or objects like Date() so that I can perform some action and then invoke the original method? So basically if I have something like var a = new Date(); I want it to (say) alert the value of the date everything Date() is called and then execute the default date constructor.
View 3 Replies
View Related
Feb 7, 2011
The goal is for selectedDay to be assigned the value of the system variable Mainpackage.subclass0.subclass1.firstDay and then incremented by two days. Mainpackage.subclass0.subclass1.firstDay needs to be unchanged.
Important system variable in red. To be 'manipulated and used' variable in green. Even with an intermediary, third, dummy variable it doesn't work:
Console log is:
It doesn't work in my webapp : All variables change at the same time.
View 4 Replies
View Related
Jun 15, 2011
I was wondering how you would create a javascript Date object from an ISO8601 string? We have done this successfully in FF4, but not IE.
If we try the following in FF4 it works:
alert(new Date("20091107T12:15:00))
In IE it returns:
'NaN'
What would be the Date standard used in JavaScript and AJAX from the server to the client? We were thinking about using UTC
View 1 Replies
View Related
May 3, 2010
I have a question about what the most certain method to obtain the correct UTC time in javascript is.On my forum, there is a countdown clock, that should show the same amount, wherever the user is located. I use UTC in javascript to get the current time, but I have trouble getting consistent results.I have tried two methods:Method 1:
Code:
var d = new Date();
var localTime = d.getTime();
[code]......
View 7 Replies
View Related
Mar 6, 2009
In the below, I would expect this to return the number of milliseconds since the epoch. Instead, it's generating a massive negative number. I presume I've gone wrong with the set[Blah] methods.
javascript Code:
var date_text = '2009-03-06 12:00:00';
var d_tried = new Date();
d_tried.setYear(parseInt(date_text.substr(0,3)));
d_tried.setMonth(parseInt(date_text.substr(5,6)));
d_tried.setDate(parseInt(date_text.substr(8,9)));
[Code]....
View 3 Replies
View Related
Apr 27, 2010
I am trying to use my date Object function to get the date from the comp but my variables in my function are coming up undefined.
Here is my function:
function displayTime() {
var currentTime, hour, minute, second, range, m, s
currentTime = new Date()
var hourInt = currentTime.getHours()
var minuteInt = currentTime.getMinutes()
var secondInt = currentTime.getSeconds()
if (hourInt > 12) {
range = "PM"
hourInt -= 12
} else {
range = "AM"
}
hour = String(hourInt)
if (minuteInt < 10) {
m = String(0) + String(minuteInt)
} minute = m
if (secondInt < 10) {
s = String(0) + String(secondInt)
} second = s
document.timeForm.timeBox.value = hour + ":" + minute + ":" + second + " " + range
alert(hour + ":" + minute + ":" + second + " " + range)
View 1 Replies
View Related
Dec 6, 2002
Well, this is acutally a couple different scripts. First is the custom date object myDate which retrieves pretty much every value that you'd need from a date and sets it as a property. If no argument is supplied, the current time and date is used. If you do supply an argument, you can send an existing javascript Date object, or a string that is compatible with declaring a normal javascript Date object.
The 2nd part of this script is the method getDate (and some accompanying String methods) that allow you to retrieve the date as a string by passing it a format string just like the PHP date() (http://www.php.net/manual/en/function.date.php) function accepts. The code posted below includes sufficient (I think) examples to help you understand what is going on. You will notice that I didn't duplicate EVERY code over from PHP, and a few are slightly different. Those that I left out I felt weren't very necessary, or I didn't feel like doing the algorithm for them :D.
Both myDateObj.getDate() and PHP's date() allow you to include text into the string that you don't want parsed, but each handles it differently. In PHP, you need to escape characters normally with a backslash. For getDate you need to proceed each character with a pipe "|". So, instead of$dateStr = date("Day z");as it would be in PHP, you would usevar d = new myDate();
var dateStr = d.getDate("D|a|y| z");There are more examples of how this works below.
Note: There is also a nice arrayReplace() method for strings that is handy for doing multiple replace() operations all at once, so I guess this is really like 3 handy scripts ;)
<html>
<head>
<title>test</title>
<meta http-equiv="expires" content="0">
<script type="text/javascript">
/*** First Part of Script, the custom date object ***/
function myDate(dateStr) {
if (typeof dateStr != 'undefined') {
var d = new Date(dateStr);
this.dateString = dateStr;
}
else if (typeof dateStr == 'object')
var d = dateStr;
else
var d = new Date();
var months = ['January','February','March','April','May','June','July','August','September','October','November',' December'];
var weekDays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var monthDays = [31,28,31,30,31,30,31,31,30,31,30,31];
this.epoch = d.getTime();
this.epochS = Math.round(this.epoch / 1000);
this.year4 = d.getFullYear();
this.year2 = parseInt(this.year4.toString().substring(2));
this.leap = (this.year4 % 400 == 0) ? true : (this.year4 % 4 == 0 && this.year4 % 100 != 0) ? true : false;
if (this.leap) monthDays[1]++;
this.mnth = d.getMonth();
this.month = this.mnth + 1;
this.month_2 = leadingZero(this.month);
this.monthName = months[this.mnth];
this.monthNameShort = this.monthName.substring(0,3);
this.days = monthDays[this.mnth];
this.dy = d.getDay();
this.day = this.dy + 1;
this.dayName = weekDays[this.dy];
this.dayNameShort = this.dayName.substring(0,3);
this.date = d.getDate();
this.date_2 = leadingZero(this.date);
this.suffix = (this.date % 10 == 1 ) ? "st" : (this.date % 10 == 2) ? "nd" : (this.date % 10 == 3) ? "rd" : "th";
this.hours24 = d.getHours();
this.hours24_2 = leadingZero(this.hours24)
this.hours12 = (this.hours24 == 0) ? 12 : (this.hours24 > 12) ? this.hours24-12 : this.hours24;
this.hours12_2 = leadingZero(this.hours12);
this.minutes = d.getMinutes();
this.minutes_2 = leadingZero(this.minutes);
this.seconds = d.getSeconds();
this.seconds_2 = leadingZero(this.seconds);
this.millis = d.getMilliseconds();
this.ampm = (this.hours24 == 0) ? "am" : (this.hours24 >= 12) ? "pm" : "am";
this.GMTstring = d.toGMTString();
this.offset = d.getTimezoneOffset();
function leadingZero(num) {
return (num < 10) ? "0" + num : num.toString();
}
}
myDate.prototype.getDayOfYear = function() {
var total = 0;
var monthDays = [31,28,31,30,31,30,31,31,30,31,30,31];
if (this.leap) monthDays[1]++;
for (var i=0; i<this.mnth; total += monthDays[i++]) {}
total += this.date;
return total;
}
/*** Second part of script, the PHP-like parsed output ***/
myDate.prototype.getDate = function(str) {
var p = ['a','A','B','d','D','F','g','G','h','H','i','I','j','l','L','m','M','n','O','r','s','S','t','T','U', 'w','W','Y','y','z','Z'];
var r = new Array();
var i = 0;
var delChar = "|";
var sNull = "null".delimit(delChar)
r[i++] = this.ampm.delimit(delChar);
r[i++] = this.ampm.toUpperCase().delimit(delChar);
r[i++] = sNull;
r[i++] = this.date_2;
r[i++] = this.dayNameShort.delimit(delChar);
r[i++] = this.monthName.delimit(delChar);
r[i++] = this.hours12;
r[i++] = this.hours24;
r[i++] = this.hours12_2;
r[i++] = this.hours24_2;
r[i++] = this.minutes_2;
r[i++] = sNull;
r[i++] = this.date;
r[i++] = this.dayName.delimit(delChar);
r[i++] = (this.leap)?1:0;
r[i++] = this.month_2;
r[i++] = this.monthNameShort.delimit(delChar);
r[i++] = this.month;
r[i++] = this.offset;
r[i++] = this.GMTstring.delimit(delChar);
r[i++] = this.seconds_2;
r[i++] = this.suffix.delimit(delChar);
r[i++] = this.days;
r[i++] = sNull;
r[i++] = this.epochS;
r[i++] = this.dy;
r[i++] = sNull;
r[i++] = this.year4;
r[i++] = this.year2;
r[i++] = this.getDayOfYear();
r[i++] = sNull;
for (i=0; i<p.length; i++)
p[i] = "/" + p[i] + "(?!|)/g";
return str.arrayReplace(p, r).replace(/|/g,"");
}
String.prototype.arrayReplace = function(arrP, arrR) {
var p, s = this;
for (var i=0; i<arrP.length; i++) {
var flags = arrP[i].substring(arrP[i].lastIndexOf("/")+1);
var regex = arrP[i].substring(1,arrP[i].lastIndexOf("/"));
p = new RegExp(regex, flags);
s = s.replace(p, arrR[i]);
}
return s;
}
String.prototype.delimit = function(char) {
var s = "";
for (var i=0; i<this.length; i++) {
s += this.charAt(i) + char;
}
return s;
}
</script>
</head>
<body>
<script type="text/javascript">
/*** Example using current date/time ***/
var md1 = new myDate();
document.write(md1.getDate("l jS of F Y h:i:s A")+"<br>");
document.write(md1.getDate("T|o|d|a|y| i|s| m.d.y")+"<br>");
document.write(md1.getDate("D M j G:i:s Y")+"<br>");
document.write("It " + (md1.leap?"is":"is not") + " a leap year<br>");
document.write("The unix epoch occurred " + md1.epochS + " seconds ago");
document.write("<hr>");
/*** Example using javascript Date object as argument ***/
var d1 = new Date(2000, 01, 02, 15, 15, 15);
var md2 = new myDate(d1);
document.write(md2.getDate("l jS of F Y h:i:s A")+"<br>");
document.write(md2.getDate("T|o|d|a|y| i|s| m.d.y")+"<br>");
document.write(md2.getDate("D M j G:i:s Y")+"<br>");
document.write("It " + (md2.leap?"is":"is not") + " a leap year<br>");
document.write("The unix epoch occurred " + md2.epochS + " seconds ago");
document.write("<hr>");
/*** Example using Date object compatible string as argument ***/
var md3 = new myDate("December 10, 1978 12:23:00");
document.write(md3.getDate("l jS of F Y h:i:s A")+"<br>");
document.write(md3.getDate("T|o|d|a|y| i|s| m.d.y")+"<br>");
document.write(md3.getDate("D M j G:i:s Y")+"<br>");
document.write("It " + (md3.leap?"is":"is not") + " a leap year<br>");
document.write("The unix epoch occurred " + md3.epochS + " seconds ago");
</script>
</body>
</html>
View 4 Replies
View Related
Feb 7, 2011
The goal is for selectedDay to be assigned the value of the system variable Mainpackage.subclass0.subclass1.firstDay and then incremented by two days. Mainpackage.subclass0.subclass1.firstDay needs to be unchanged.
Important system variable in red.
To be 'manipulated and used' variable in green.
Even with an intermediary, third, dummy variable it doesn't work:
Code:
//Declarations and instantiations
var systemDate = new Date();
var selectedDay = new Date();[code]....
View 1 Replies
View Related
Jun 12, 2006
Heres my most current code. I have it where, onClick on an image it calls this. and it works, but the hp keeps restarting, like they all minus from 100, instead of the number that prieviously minused from 100. For xp: 100-9=91, is the first, the second, 100-72=28 instead of 91-72. How do i fix
var myhp;
function takehits()
{
var dmg= new Array()
dmg[0]=9;
dmg[1]=11;
dmg[2]=15;
dmg[3]=21;
dmg[4]=29;
myhp=100;
var rndtake=Math.floor(Math.random()*5);
document.getElementById('health').innerHTML=myhp-dmg[rndtake];
}
I want to have it where, you have an aray, and u have a number value that you want the number value, to be subtracted by one of the numbers in the array. And u lets say the array has 5 choices. Code:
View 4 Replies
View Related
Feb 6, 2010
I have a start date and end date text boxs. What I would like to achieve is when a submit button is clicked all the available dates between start and end dates should be displayed together with 3 check boxes next to each date (please see below). I am just wondering whether that'sachievablewith jquery, and if so, how I might be able to implement this.
Start date End date
View 8 Replies
View Related
Mar 16, 2011
I am trying to change the script below from adding the values to subtracting without much luck.
Code:
<script type="text/javascript" language="javascript">
function autocalc(oText) {
if (isNaN(oText.value)) //filter input {
alert('Numbers only!');
oText.value = '';
} var field, val, oForm = oText.form, netincome = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements {
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number? {
netincome += val; //accumulate
}}
oForm.netincome.value = netincome; //out
}
</script>
View 1 Replies
View Related
Apr 19, 2011
What Ive done is use local storage to set a value in my web app.
Then my next task was to write said value on my page (This took me hours but finally got it done).
My next task is using Setinterval(or what ever the correct way to solve my problem) to subtract a number (lets use "1" for this example) Every 5 seconds from the local storage value.So basically After I click the button. Value is set and displayed. After another button is click. Every 5 seconds the localstorage is subtraced by 1. I think the code im looking for is similar to this
Code:
View 1 Replies
View Related