Date without time for toLocaleString

R

RandyTh

When calling the function toLocaleString(), javascritp date object returns
yyyy/mm/dd HH:MM:SS depending on the locale settings. Is it possible to
just display date as yyyy/mm/dd without the time ?

SK
 
T

Thomas 'PointedEars' Lahn

RandyTh said:
When calling the function toLocaleString(), javascritp date object returns
yyyy/mm/dd HH:MM:SS depending on the locale settings. Is it possible to
just display date as yyyy/mm/dd without the time ?

Sure, you just need to change your locale :)

Seriously, try this quickhack:

var
d = new Date(...),
month = d.getMonth() + 1,
day = d.getDate();

if (!Date.prototype.getFullYear)
{
Date.prototype.getFullYear = function()
{
var y = this.getYear();
return (y < 1900 ? y + 1900 : y);
}
}

[d.getFullYear(),
(m < 10 ? "0" + month : month),
(d < 10 ? "0" + day : day)].join("/")

However, I strongly recommend to stick to the ISO date format
(YYYY-MM-DD) to avoid any ambiguities regarding `yyyy/mm/dd'.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[d.getFullYear(),
(m < 10 ? "0" + month : month),
(d < 10 ? "0" + day : day)].join("/")
^
Sorry, should have been

[d.getFullYear(),
(m < 10 ? "0" + month : month),
(day < 10 ? "0" + day : day)].join("/")


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[d.getFullYear(),
(m < 10 ? "0" + month : month),
(d < 10 ? "0" + day : day)].join("/")
^
Sorry, should have been

[d.getFullYear(),
(month < 10 ? "0" + month : month),
(day < 10 ? "0" + day : day)].join("/")


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 27 Nov
2005 12:55:10, seen in Thomas 'PointedEars'
Lahn said:
if (!Date.prototype.getFullYear)
{
Date.prototype.getFullYear = function()
{
var y = this.getYear();
return (y < 1900 ? y + 1900 : y);
}
}


AIUI, that is not correct for at least one browser, which implements
getYear() to give (at least around 2000) what getFullYear()%100 gives.

However, one can write a function getFY - not much longer than the
above - that is safe on any browser for which getYear()%100 is
always right. Read the newsgroup FAQ; see below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top