/    Sign up×
Community /Pin to ProfileBookmark

How to calculte the date a week ago

I am using UTC() to get the number of millisecs since Jan 1,1970 for “today”, subtracting 604800 (60*60*24*7) and using this new value (calculated as a var,lastweek) in new Date(lastweek) in order to get the date a week ago. The result I get is today’s values for the date, not those for a week ago. Please, what am I doing wrong? How can I get the date a week ago?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJan 10.2005 — [i]Originally posted by Krumbly [/i]

[B]I am using UTC() to get the number of millisecs since Jan 1,1970 for "today", subtracting 604800 (60*60*24*7)[/B][/QUOTE]


The value [FONT=courier new]60*
60*24*7[/FONT] would be the number of [i]seconds[/i] in a week, not miliseconds. You need to add a [FONT=courier new]1000*[/FONT] to the front:

<script type="text/javascript">
<!--
var today = new Date();
var lastWeek = new Date(today.getTime()-[color=red]1000*[/color]60*60*24*7);
alert("Today: " + today + "nLast Week: " + lastWeek);
//-->
</script>
Copy linkTweet thisAlerts:
@CharlesJan 10.2005 — Another option is to use the Date object's error handling to your advantage:

[font=monospace]<script type="text/javascript">

<!--

then = new Date();

then.setDate (then.getDate() - 7);

alert (then.toDateString());

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@ExuroJan 10.2005 — I don't understand what that has to do with error handling... Would you mind explaining?

[b]Unrelated Sidenote to Charles:[/b]

I believe the title you're looking for is [i]senpai[/i] rather than [i]sempai[/i]. I don't know why it happens, but I've heard several Japanese words that get the "n" switched for an "m". For example, "numchucks" are actually "nunchucks" from the Japanese word [i]nunchaku[/i].

Just out of curiosity, are you saying you're a [i]senpai[/i] here at the forums, being one older and more experienced and lending help to those less experienced? Or does it actually have something to do with the samurai/kendo picture you have as your avatar?
Copy linkTweet thisAlerts:
@CharlesJan 10.2005 — With the JavaScript Date object, the incorrect date "-2 January 2005" becomes 29 December 2004.

"Sempai" is how my school transliterates the word. 20 people are more senior than I on this board and only four have more posts. I needed a word that went better with my Avatar, which I picked largely because of the colors. "Sempai" seemed apt, somehow.
Copy linkTweet thisAlerts:
@KrumblyauthorJan 10.2005 — Thanks Exuro. I wish I could claim the missing multiplier (sec to millisecs) was a deliberate error, but it was just a stupid oversight.

And thanks Charles, your:

then = new Date();

then.setDate (then.getDate() - 7);

alert (then.toDateString());

is what I wanted but couldn't get to work (I'm checking that a date entered, in a form, is at least a week ago).
Copy linkTweet thisAlerts:
@Warren86Jan 10.2005 — <HTML>

<Head>

<Script Language=JavaScript>

var currDate = 0;

function calcJulian(isDate){

isValid = true;
tmp = isDate.split("/");
if (tmp[0] <1 || tmp[0] >12 || tmp[1] <1 || tmp[1] >31 || tmp[2] < 1970){isValid = false};
if ((tmp[0] == 4 || tmp[0] == 6 || tmp[0] == 9 || tmp[0] == 11) && tmp[1] > 30){isValid = false}
if ((tmp[0] == 1 || tmp[0] == 3 || tmp[0] == 5 || tmp[0] == 7 || tmp[0] == 8 || tmp[0] == 10 || tmp[0] == 12) && tmp[1] > 31){isValid = false}
isLeap = tmp[2]%4;
if ((tmp[0] == 2 && isLeap == 0) && tmp[1] > 29){isValid = false}
if ((tmp[0] == 2 && isLeap != 0) && tmp[1] > 28){isValid = false}
if (isNaN(Date.parse(isDate)) || !isValid){alert("Invalid Date"); return false}
else {
gregDate = new Date(isDate);
year = gregDate.getFullYear();
month = gregDate.getMonth()+1;
day = gregDate.getDate();
A = Math.floor((7*(year+Math.floor((month+9)/12)))/4);
B = day+Math.floor((275*month)/9)
isJulian = (367*year)-A+B+1721014;
return isJulian
}
}

function checkDate(isDate){

isValid = true;
if (calcJulian(isDate.value))
{
inputDate = calcJulian(isDate.value)
daysApart = currDate-inputDate;
if (daysApart < 7){isValid = false}
}
if (!isValid){alert('Invalid Date');isDate.value = ""; isDate.focus()}
if (isValid){alert('Valid Date')}
}

function getToday(){

gregDate = new Date();
year = gregDate.getFullYear();
month = gregDate.getMonth()+1;
day = gregDate.getDate();
A = Math.floor((7*(year+Math.floor((month+9)/12)))/4);
B = day+Math.floor((275*month)/9)
isJulian = (367*year)-A+B+1721014;
currDate = isJulian;
}

window.onload=getToday;


</Script>

</Head>

<Body>

<Form name='Form1'>

Input Date (mm/dd/yyyy): <input type=text size=9 name='nDate' onblur="checkDate(this)"><br>

Nothing: <input type=text size=9>

</Form>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@CharlesJan 10.2005 — [font=monospace]<script type="text/javascript">

<!--

Date.prototype.isHoliday = function () {

var holidays = [new Date ('1 January 2005'), new Date ('2 February 2005'), new Date ('25 December 2005')];

var h, i = 0;

while (h = holidays[i++]) {if (this.getTime() == h.getTime()) return true}

return false;

}

Date.prototype.isWeekend = function () {return this.getDay() == 0 || this.getDay() == 6}

Date.prototype.plusWorkDays = function (n) {

if (n == 0) return this;

var d = new Date (this.getTime());

var i = 0;

if (n > 0) {

while (i < n) {

d.setDate(d.getDate() + 1);

if (!(d.isWeekend() || d.isHoliday())) {i++}

}

} else {

while (i > n) {

d.setDate(d.getDate() - 1);

if (!(d.isWeekend()|| d.isHoliday() )) {i--}

}

}

return d;

}

alert (new Date('31 January 2005').plusWorkDays (10));

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@ExuroJan 10.2005 — [i]Originally posted by Charles [/i]

[B]With the JavaScript Date object, the incorrect date "-2 January 2005" becomes 29 December 2004.[/B][/QUOTE]

Ah, that makes sense. I love it when scripts do that! It makes coding so much easier when the dates will auto-correct themselves. Blackbox all the way!

[i]Originally posted by Charles [/i]

[B]"Sempai" seemed apt, somehow. [/B][/QUOTE]

Fine then, spurn my four years of Japanese :p
×

Success!

Help @Krumbly spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...