I made a short function, which aims to provide the date of the next day.
I do not know why, but in my version with the date of 28.02.2009 on the
function returns the result to me on 0-11-2009. Where do I have wrong?
var gdate = "28-02-2009"; // example date
var dateelm = gdate.split('-');
var day = dateelm[0];
var month = dateelm[1];
var year = dateelm[2];
var testDate = new Date(year, month, (day+1));
var nextD = [testDate.getDay(), testDate.getMonth(),
testDate.getFullYear()];
return nextD.join("-"); // return date of next day
--
Peter
You're adding one to the day (on line 6) without taking into account
that it could exceed the number of days in that month. For example, if
day is 31, you'd be incrementing it to 32. Same deal with 28 in February
on a non leap year.
Your day is a string at this point.
> var month = dateelm[1];
> var year = dateelm[2];
> var testDate = new Date(year, month, (day+1));
<snip>
When either operand to the + operator is a string the operation
performed is concatenation (with any non-string operands type-
converted into strings. The effect of - (day+1) - is the same as the
effect of - ("28" + "1") -, which is the string "281". The Date
constructor probably type-converts this into a number that it uses as
the number of days specified, but the 281st day of March is some time
in November.
Warning: month numbers used in/with javascript Date objects are zero
based (January is zero).
When writing functions guiding the documentation from
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
and examples from Google. I thought that the date will know about it.
However, even tried by:
var gdate = "28-02-2009"; // example date
var dateelm = gdate.split('-');
var day = dateelm[0];
var month = dateelm[1];
var year = dateelm[2];
var testDate = new Date(year, month, day);
testDate.setDate(testDate.getDate()+1);
var nextD = [testDate.getDay(), testDate.getMonth(),
testDate.getFullYear()];
return nextD.join("-"); // return date of next day
As a result of getting 0-2-2009. Hm...
--
Peter
getDate will work better there.
Argh... You right.
--
Peter
So, I have to convert string to integer using parseInt.
> Warning: month numbers used in/with javascript Date objects are zero
> based (January is zero).
I forgot. Now, the complete working functions is:
gdate = "28-02-2009";
var day = parseInt(gdate[0])+1;
var month = parseInt(gdate[1])-1;
var year = parseInt(gdate[2]);
var testDate = new Date(year, month, day);
// array of dd, mm, yyy
var nextDay = [
((testDate.getDate())<10 ? "0" : "")+(testDate.getDate()),
((testDate.getMonth())<10 ? "0" : "")+(testDate.getMonth()+1),
testDate.getFullYear()
];
return nextDay.join("-");
Thanks for advices.
--
Peter
try:
var gdate = "28-02-2009"; // example date
var dateelm = gdate.split('-');
var day = dateelm[0];
var month = dateelm[1];
var year = dateelm[2];
var nextDate = new Date(year, --month, ++day);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
getDay() returns the day of the week (0 = sunday). Use getDate() instead.
Matt
> var nextDay = [
> ((testDate.getDate())<10 ? "0" : "")+(testDate.getDate()),
> ((testDate.getMonth())<10 ? "0" : "")+(testDate.getMonth()+1),
> testDate.getFullYear()
> ];
> return nextDay.join("-");
Alternative:
var today = new Date();
var tomorrow = [
(today.getDate()+101).toString().substr(1),
(today.getMonth()+101).toString().substr(1),
today.getFullYear()
].join('-');
alert(tomorrow);
Not like that. If you use - parseInt - it is an extremely good idea to
get into the habit of providing its second (optional) "radix"
argument. Without that - parseInt - is allowed to chose the base that
it will use in interpreting its input. Specifically, strings that
start with "0x" will be interpreted as hexadecimal and strings that
start with "0" (not followed by "x") MAY be interpreted as octal
numbers. This is particularly problematic if your number strings
originate from dates as leading zeros are then likely.
If you must use - parseInt - then the form:-
var day = parseInt(gdate[0], 10)+1;
- would be safer, but plain type-conversation of the string will
assume base 10 and disregard leading zeros so either of:-
var day = Number(gdate[0])+1;
- or:-
var day = (+gdate[0], 10)+1; // Applying the unary + operator
// to the string.
- will be effective (with the last being by far the quickest).
>
> var day = (+gdate[0], 10)+1; // Applying the unary + operator
> // to the string.
>
That SyntaxError appears to be the result of copy and paste.
> - will be effective (with the last being by far the quickest).
gdate[0] can be converted to a number (possibly NaN) with the unary +:-
var day = +gdate[0];
Garrett
--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
That is a copy-paste error, but not a syntax error (comma operator is
fine in context). It should have been:-
var day = (+gdate[0]) + 1;
Henry wrote/zu Deiner Priorität-Alpha-1-Nachricht von Sternzeit 02/24/2009
05:14 PM:
Yes.
> but plain type-conversation of the string will
> assume base 10 and disregard leading zeros
No, it won't.
var s = "0x12";
// yields 18
+s
// yields 18
Number(s)
Tested in Firefox/Iceweasel (JavaScript 1.8) on Debian GNU/Linux.
> so either of:-
>
> var day = Number(gdate[0])+1;
>
> - or:-
>
> var day = (+gdate[0], 10)+1; // Applying the unary + operator
> // to the string.
>
> - will be effective (with the last being by far the quickest).
The unary plus may be the quickest, but since it works like Number() and
thus considers the prefix, I don't think the last two suggestions are
prudent to follow here.
PointedEars
You need to make the indicated changes :
var gdate = "28-02-2009"; // example date
var dateelm = gdate.split('-');
var day = dateelm[0];
var month = dateelm[1];
var year = dateelm[2];
var testDate = new Date(year, month-1, +day+1);
// ^^^^^^^ ^^^^^^
var nextD = [testDate.getDate(), testDate.getMonth()+1, testDate.getFullYear()];
// ^^^^^^^ ^^
return nextD.join("-"); // return date of next day
Note that unary + converts a String to a Number. Using parseInt on a
decimal digit string is unnecessary.
You should also use nextD.join("-").replace(/\b(\d)\b/g, "0$1");
or don't bother with an array and use a Leading Zero function.
IMHO, one should not, unless forced by local convention, use the ISO 8601
date field separator "-" without also using the ISO 8601 date field
order YYYY MM DD.
You could use
var gdate = "28-02-2009"; // example date
var D = new Date(gdate.replace(/(..).(..).(....)/, "$3/$2/$1"))
D.setDate(D.getDate()+1)
D.toLocaleString()
replacing the last line ATSR.
DATE object is updated.
Read the sig below, slowly.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Thomas, can you explain why you are using hexadecimal numbers in a date
string?
I am unfamiliar with this practice.
> Tested in Firefox/Iceweasel (JavaScript 1.8) on Debian GNU/Linux.
>
Apparently, so is the Date constructor/function.
new Date("1/0xf/2009");
"invalid date".
But that isn't stopping PointedEars...
>> so either of:-
>>
>> var day = Number(gdate[0])+1;
>>
>> - or:-
>>
>> var day = (+gdate[0], 10)+1; // Applying the unary + operator
>> // to the string.
>>
>> - will be effective (with the last being by far the quickest).
>
> The unary plus may be the quickest, but since it works like Number() and
> thus considers the prefix, I don't think the last two suggestions are
> prudent to follow here.
>
Backing up to the first post, I see that gdate[0] would have a good
chance of being "08" or "09". If that could be interpreted as octal,
that would result in an error two days out of the month and 2 months out
of the year ("08" and "09").
The relevant section is 9.3.1, "ToNumber Applied to the String Type"
| StrNumericLiteral :::
| StrDecimalLiteral
| HexIntegerLiteral
Further down:
| DecimalDigit ::: one of
| 0 1 2 3 4 5 6 7 8 9
It is unsafe to use unary + whenever hexadecimal date strings are used.
But what do I know. I still use decimal for date strings.
> var testDate = new Date(year, month-1, +day+1);
var testDate = new Date(year, --month, ++day);
> You should also use nextD.join("-").replace(/\b(\d)\b/g, "0$1");
> or don't bother with an array and use a Leading Zero function.
.replace(/\b(\d)\b/g, "0$1")
Nice, John.
> IMHO, one should not, unless forced by local convention, use the ISO 8601
> date field separator "-" without also using the ISO 8601 date field
> order YYYY MM DD.
"forced by local convention" indeed,
this is the most common notation overhere.
Oops, that's half-broken, isn't it ?
See:
javascript:s= "0x12";alert((+s)+ ","+ parseInt(s,16)); //-> 18,18
but:
javascript:s= "-0x12";alert((+s)+ ","+ parseInt(s,16)); //-> NaN,-18
--
Jorge.
...in Safari: bug# 24156.
--
Jorge.
Thank You very much for suggestions.
> Read the sig below, slowly.
Why slowly? I see there what you want.
--
Peter
It is still a small problem. The function can accept parameters of the
date of 1-3-2009, but also 01-3-2009, 1-03-2009, and the "replace" does
not take into account.
--
Peter
I was changed:
var D = new Date(gdate.replace(/(..).(..).(....)/, "$3/$2/$1"))
to:
var D = new Date(gdate.replace(/(\d{1,2}).(\d{1,2}).(\d{1,4})/,
"$3/$2/$1"));
to works with all cases above described.
--
Peter
var d = new Date(gdate.replace(/(\d{1,2}).(\d{1,2}).(\d{4})/, "$3/$2/$1"));
That'll accept one or two digits as date and month.
And since D is not a constructor I replaced it by d :-)
Matt
>> but plain type-conversation of the string will
>> assume base 10 and disregard leading zeros
>
>No, it won't.
Henry is referring to the OP's data and not to strings in general. The
OP has not validated his input as looking like a date, and so can be
considered to be sufficiently confident that it is ##-##-####. The OP's
data contains no letters, therefore the string cannot look like Hex.
> var s = "0x12";
>
> // yields 18
> +s
>
> // yields 18
Unary + does not ever consider a following string to be in octal.
>> var day = Number(gdate[0])+1;
>>
>> - or:-
>>
>> var day = (+gdate[0], 10)+1; // Applying the unary + operator
>> // to the string.
One wonders what that 10 might be intended for. It works well for the
eleventh day of each month, but not otherwise.
>> - will be effective (with the last being by far the quickest).
>
>The unary plus may be the quickest, but since it works like Number() and
>thus considers the prefix, I don't think the last two suggestions are
>prudent to follow here.
Unary + **SHOULD** consider "0x12" to be eighteen; it would be foolish
to return zero or seven hundred and thirty-two, and probably unwise to
return NaN. if those effects are wanted, they can be obtained by other
means.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
I don't think so, having read his response in context:
>>> [...] Specifically, strings that
>>> start with "0x" will be interpreted as hexadecimal and strings that
>>> start with "0" (not followed by "x") MAY be interpreted as octal
>>> numbers. This is particularly problematic if your number strings
>>> originate from dates as leading zeros are then likely.
>>>
>>> If you must use - parseInt - then the form:-
>>>
>>> var day = parseInt(gdate[0], 10)+1;
>>>
>>> - would be safer, but plain type-conversation of the string will
>>> assume base 10 and disregard leading zeros so either of:-
>>>
>>> var day = Number(gdate[0])+1;
>>>
>>> - or:-
>>>
>>> var day = (+gdate[0], 10)+1; // Applying the unary + operator
>>> // to the string.
>>>
>>> - will be effective (with the last being by far the quickest).
PointedEars
The original article gave a date with a leading zero, so I showed code
for that case. Consider these.
D = new Date(gdate.replace(/(\d+).(\d+).(\d+)/, "$3/$2/$1"))
D = new Date(gdate.replace(/(.+)\D(.+)\D/, "$2/$1/"))
D = new Date(gdate.replace(/\D+/g, "/")) // needs Y-M-D
One of the things that the FAQ does not yet, AFAIR, say is that
undertaking string manipulation without considering RegExps is STUPID.
It takes very little time to learn enough for simple things as above.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
>It is unsafe to use unary + whenever hexadecimal date strings are used.
>But what do I know. I still use decimal for date strings.
Unary + works perfectly well with positive hexadecimal strings; it
treats them as hexadecimal. In better browsers, it also works with
negative ones.
That might be worth a FAQ mention.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
In the textarea at the bottom of
<URL:http://www.merlyn.demon.co.uk/js-date8.htm>,
put D = new DATE(Str) where string Str contains a Dutch numeric
date, and see what happens when the button is pressed. Then try D =
new DATE("~"+Str) .
I'm still wondering about reading a date string with a year less than
100; hoping for an elegant fix. Reading a valid ISO 8601 Ordinal Date
is wonderfully easy with new Date and a simple RegExp.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
New : 32-bit COLS, LONGCALC, CHEKLINX in new directory.
LOL, in the best it doesn't !
________^^^^^^^^_____________
--
Jorge.
True. I noticed the error only in the later tests.
> D = new Date(gdate.replace(/(\d+).(\d+).(\d+)/, "$3/$2/$1"))
> D = new Date(gdate.replace(/(.+)\D(.+)\D/, "$2/$1/"))
> D = new Date(gdate.replace(/\D+/g, "/")) // needs Y-M-D
>
> One of the things that the FAQ does not yet, AFAIR, say is that
> undertaking string manipulation without considering RegExps is STUPID.
> It takes very little time to learn enough for simple things as above.
Have created exactly the same as provided in the "point" 1.
--
Peter
IOW, in Opera, FF and Chrome negative hexadecimals are handled fine.
Not so in Safari, nevertheless, as "it also works with negative ones"
isn't in the ECMA spec... (!), it isn't a bug.
Mira, mira:
Not even with "This is particularly problematic if your number strings
originate from dates as leading zeros are then likely" preceding
descriptions of what could be done to address the problem?
Of course; it is the set of acceptable inputs which is changed.
DATE is doing well, in js-date8.
--