Click to See Complete Forum and Search --> : Date format in js


shanuragu
09-04-2003, 06:01 AM
Hi
How can I format date in js?

shara

Charles
09-04-2003, 06:44 AM
<script type="text/javascript">
<!--
alert (new Date()); // formats date and time according to some strange standard

alert (new Date().toString()); // formats date and time according to that same strange standard

alert (new Date().toLocaleString()); // formats date and time according to the user's locale settings

alert (new Date().toDateString()); // formats date according to the user's locale settings

alert (new Date().toTimeString()); // formats time according to the user's locale settings

// and you can overwrite any of the above methods to your liking:
Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join(' ')}

alert(new Date());
// -->
</script>

See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/date.html.

shanuragu
09-04-2003, 08:57 AM
How can I format string date in the format "dd/mmm/yyyy" in to dd/mm/yyyy Date format??

urgent please help.

shara

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

alert(new Date('9/may/2003'.replace(/\//g, ' ')).toDateString());
// -->
</script>

If I understand what you mean by "dd/mmm/yyyy" its a non-standard format and you'll need to replace the "/" character with spaces, as above.