By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

sysdate function

P: n/a
Max
I need a little javascript function that returns the date of today in the
format dd/mm/yyyy
example:
September 2, 2003 ==> 02/09/2003

For control reasons, it cannot be 2/9/2003 or 02/9/2003 or 2/09/2003 but
only 02/09/2003

December 14, 2003 ==> 14/12/2003

Thanks
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Lee
Max said:

I need a little javascript function that returns the date of today in the
format dd/mm/yyyy
example:
September 2, 2003 ==> 02/09/2003

For control reasons, it cannot be 2/9/2003 or 02/9/2003 or 2/09/2003 but
only 02/09/2003

December 14, 2003 ==> 14/12/2003


Per the FAQ for this newsgroup, you should look here:
http://www.merlyn.demon.co.uk/js-dates.htm

You might also consider using the format 2003-12-14, if possible.

Jul 20 '05 #2

P: n/a
Max
doesn't help much
"Lee" <RE**************@cox.net> a écrit dans le message news:
bj********@drn.newsguy.com...
Max said:

I need a little javascript function that returns the date of today in the
format dd/mm/yyyy
example:
September 2, 2003 ==> 02/09/2003

For control reasons, it cannot be 2/9/2003 or 02/9/2003 or 2/09/2003 but
only 02/09/2003

December 14, 2003 ==> 14/12/2003


Per the FAQ for this newsgroup, you should look here:
http://www.merlyn.demon.co.uk/js-dates.htm

You might also consider using the format 2003-12-14, if possible.

Jul 20 '05 #3

P: n/a
"Max" <ce******@cgey.com> wrote in
news:bj**********@s1.read.news.oleane.net:
doesn't help much
"Lee" <RE**************@cox.net> a écrit dans le message news:
bj********@drn.newsguy.com...
Max said:
>
>I need a little javascript function that returns the date of today
>in the format dd/mm/yyyy
>example:
>September 2, 2003 ==> 02/09/2003

<html>
<head></head>
<body>
<script>
function formatDate(date,frm)
{
var str = new String(frm);
var m = date.getMonth();
var d = date.getDate();
var y = date.getFullYear();
var w = date.getDay();
var s = new Array();
s["d"] = d;
s["dd"] = (d < 10) ? ("0" + d) : d;
s["M"] = 1+m;
s["MM"] = (m < 9) ? ("0" + (1+m)) : (1+m);
s["y"] = y;
s["yy"] = new String(y).substr(2, 2);
s["yyyy"] = y;
var re = /(.*)(\W|^)(d|dd|ddd|dddd|M|MM|MMM|MMMM|y|yy|yyyy)( \W|$)(.
*)/;
while (re.exec(str) != null) {
str = RegExp.$1 + RegExp.$2 + s[RegExp.$3] + RegExp.$4 +
RegExp.$5;
}
return str;
}

document.write(formatDate(new Date(),'dd/MM/yyyy'));

</script>
</body>
</html>

--
In theory there is no difference between theory and practice. In practice
there is. - YB
Jul 20 '05 #4

P: n/a
JRS: In article <Xn*********************@198.80.55.250>, seen in
news:comp.lang.javascript, Sean Jorden <s_***********@no.spam.n_o_r_a.d.a.com>
posted at Wed, 3 Sep 2003 07:49:46 :-
"Max" <ce******@cgey.com> wrote in
news:bj**********@s1.read.news.oleane.net:
doesn't help much
"Lee" <RE**************@cox.net> a écrit dans le message news:
bj********@drn.newsguy.com...
Max said:
>
>I need a little javascript function that returns the date of today
>in the format dd/mm/yyyy
>example:
>September 2, 2003 ==> 02/09/2003

<html>
... ... ... ...
</html>


But he specifically requested a little function.

function LZ(x) {return(x<0||x>9?"":"0")+x}

function formatDate(date) {
with (date) return LZ(getDate())+'/'+LZ(getMonth()+1)+'/'+getFullYear()
}

document.write(formatDate(new Date()))

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

P: n/a
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in
news:ca**************@merlyn.demon.co.uk:


But he specifically requested a little function.


sorry, I don't like non-reusable code.
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.