Date Time question

D

DEEAS

var DateString=(new Date("")).toString();



1-How do you get the DateString to give you the Date Time format that
you want ?
2- How can you add a few more seconds to that Date value and display
it again.
Perhaps that needs to be converted into a large value in seconds ,
added +2 seconds and then converted back to teh Date Time string.

Thanks
 
R

RobG

var DateString=(new Date("")).toString();

1-How do you get the DateString to give you the Date Time format that
you want ?

You manually get the components and format them yourself. If you let
the UA write it out itself, the ECMAScript spec says:

15.9.5.3 Date.prototype.toDateString ( )
This function returns a string value. The contents of the string are
implementation-dependent, but are intended to represent the "date"
portion of the Date in the current time zone in a convenient, human-
readable form.

In other words, it depends on the UA and user settings, you can't
control it programmatically unless you replace the Date object's
toString method (which may not be a good idea).

Date.prototype.toString = function(){
function addZ(n){return (n<10)? '0'+n:''+n;}
var d = this;
return d.getFullYear() + '/'
+ addZ(d.getMonth()+1) + '/'
+ addZ(d.getDate()) + ' '
+ addZ(d.getHours()) + ':'
+ addZ(d.getMinutes()) + ':'
+ addZ(d.getSeconds());
}


Some old browsers may not support getFullYear(), you may want to test
and provide a fallback. Ensure your dates are suitable for
international use.

2- How can you add a few more seconds to that Date value and display
it again.

Something like:

var aFewMoreSeconds = 5;
var now = new Date();
now.setSeconds(now.getSeconds() + aFewMoreSeconds);

or

now = new Date(now.getTime() + aFewMoreSeconds*1e3);

Perhaps that needs to be converted into a large value in seconds ,
added +2 seconds and then converted back to teh Date Time string.

Keep the date object as an object so you can easily work with it using
the built-in Date methods. Provide your own function to convert it to
a string only for display.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
return d.getFullYear() + '/'
+ addZ(d.getMonth()+1) + '/'
+ addZ(d.getDate()) + ' '
+ addZ(d.getHours()) + ':'
+ addZ(d.getMinutes()) + ':'
+ addZ(d.getSeconds());
}


Some old browsers may not support getFullYear(), you may want to test
and provide a fallback. Ensure your dates are suitable for
international use.

For that, the date separator ideally should be "-" rather than "/". In
principle, the "-" should be a hyphen, as distinct from a dash or minus,
in accordance with ISO 8601:2004; but \x002D may well do.

More importantly, the OP should ensure that his reader will be in no
doubt about whether the time is author's, server's, UTC, or user's.
 
D

DEEAS

In comp.lang.javascript message <[email protected]



For that, the date separator ideally should be "-" rather than "/". In
principle, the "-" should be a hyphen, as distinct from a dash or minus,
in accordance with ISO 8601:2004; but \x002D may well do.

More importantly, the OP should ensure that his reader will be in no
doubt about whether the time is author's, server's, UTC, or user's.

--
(c) John Stockton, Surrey, UK. [email protected] Turnpike v6.05 IE 6.
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.

Thanks Dr & Rob for your inputs.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top