Click to See Complete Forum and Search --> : I just can't get it to work


teija
09-12-2003, 05:45 AM
Hi

I did just like you suggested but it doesn't work?
I'm confused - is it really this hard to deal with dates in JS?

I tried to simply get the setDate -function to work, without any form fields to confuse it - but it does not work

this is what I tried:

day1 = new Date("12.9.2003");
day1.setDate(24);
alert(day1);

it gives me NaN! So I think it referres to the date format that I'm jusing! When I put ("July 27, 1962 23:30:00") in there it works - but it only chages the day to be 24 and this is not what I wanted. I wanted the function to add days to given date! I'm beginning to wonder if this is even possible with JS?

t.teija

Charles
09-12-2003, 06:10 AM
<script type="text/javascript">
<!--

// in case the browser doesn't understand European Date Format we need to define our own method.

Date.fromEDF = function (s) {var a = s.split(/[\.-]/); return new Date(a[2], --a[1], a[0])}

day1 = Date.fromEDF('12.9.2003');
day1.setDate(24);
alert(day1.toDateString());

// but if you want to add 24 days then:

Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;

alert(new Date(Date.fromEDF('12.9.2003').getTime() + 24 * Date.ONE_DAY).toDateString())
// -->
</script>

teija
09-12-2003, 07:02 AM
it returns the date in form: Sat Oct 4 2003 and i'd like it to show it in format: 4.10.2003.

I copied your code:

Date.fromEDF = function (s) {var a = s.split(/[\.-]/); return new Date(a[2], --a[1], a[0])}

day1 = Date.fromEDF('12.9.2003');
day1.setDate(24);

Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;

alert(new Date(Date.fromEDF('12.9.2003').getTime() + 24 * Date.ONE_DAY).toDateString()) // -->

thank you some much for youre patiency with me!!

teija

Charles
09-12-2003, 11:32 AM
<script type="text/javascript">
<!--
Date.prototype.toDateString = function () {return [this.getDate(), this.getMonth() + 1, this.getFullYear()].join('.')}

Date.fromEDF = function (s) {var a = s.split(/[\.-]/); return new Date(a[2], --a[1], a[0])}

Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;

alert(new Date(Date.fromEDF('12.9.2003').getTime() + 24 * Date.ONE_DAY).toDateString())
// -->
</script>

teija
09-15-2003, 12:21 AM
It works now - thank you so much for your help!

teija