getting date/time from javascript

J

jessica6_2000

I have a page where I need to display the unix time (seconds) upon
page load. I've found many complex examples that auto update, count
live, etc., but I need to just display the time at the time of page
load. I think it has something to do with getTime, showDate, or maybe
loadTime, but I can't quite seem to get the syntax.
Could someone provide the exact syntax and usage within the html to
make this work?

Thanks!
 
T

Thomas 'PointedEars' Lahn

I have a page where I need to display the unix time (seconds)
upon page load. I've found many complex examples that auto update, count
live, etc., but I need to just display the time at the time of page
load. I think it has something to do with getTime,

It does:

<body>
...
<script type="text/javascript">
document.write(
'<div>' + Math.floor((new Date()).getTime() / 1000) + '<\/div>');
</script>
...
</body>

or (slightly more efficient)

<body>
...
<script type="text/javascript">
document.write('<div>' + Math.floor(+new Date() / 1000) + '<\/div>');
</script>
...
</body>

Math.floor() is assuming that you only want full seconds (the time value
in ECMAScript implementations is the number of *milliseconds* since the
beginning of the Unix epoch).
showDate, or maybe loadTime, but I can't quite seem to get the syntax.

Those terms are pure fantasy. RTFM before posting would have helped.

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Date


PointedEars
 
E

Evertjan.

wrote on 26 okt 2007 in comp.lang.javascript:
I have a page where I need to display the unix time (seconds) upon
page load. I've found many complex examples that auto update, count
live, etc., but I need to just display the time at the time of page
load. I think it has something to do with getTime, showDate, or maybe
loadTime, but I can't quite seem to get the syntax.

Since "unix time" has different flavours, let me give you this:

var milisecs = +new Date();

gives you the exact number of miliseconds since

var start = new Date(0); // 1 jan 1970 00:00:00:000 UTC

on the local machine.
Could someone provide the exact syntax and usage within the html to
make this work?

So you are not inclined to do some programming yourself?

It seems you need a paid programmer.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
I have a page where I need to display the unix time (seconds)
upon page load. I've found many complex examples that auto update, count
live, etc., but I need to just display the time at the time of page
load. I think it has something to do with getTime,

It does:
[...]
or (slightly more efficient)

<body>
...
<script type="text/javascript">
document.write('<div>' + Math.floor(+new Date() / 1000) + '<\/div>');

Because of the division operator that already converts to Number,

document.write('<div>' + Math.floor(new Date() / 1000) + '<\/div>');

suffices here. Implicit type conversion -- a blessing and a curse :)


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
var milisecs = +new Date();

gives you the exact number of miliseconds since

var start = new Date(0); // 1 jan 1970 00:00:00:000 UTC

on the local machine.

Strictly, not quite exactly.

The number of milliseconds is incremented at an interval which may
average 54.9, 15.625, 10, or other number of milliseconds. Sometimes,
that matters.

And it's the number of milliseconds UT or GMT; UTC has Leap Seconds too.

Using (new Date()/1000)|0 will do for a bit over 30 years
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 26 okt 2007 in comp.lang.javascript:
Because of the division operator that already converts to Number,

document.write('<div>' + Math.floor(new Date() / 1000) + '<\/div>');

suffices here. Implicit type conversion -- a blessing and a curse :)

Since unix time wants to be written in a special format
like: 1 193 433 406
<http://en.wikipedia.org/wiki/Unix_time>,
try:


<script type='text/javascript'>

var re = /^(\d)(\d{3})(\d{3})(\d{3})\d{3}$/
var x = (+new Date()+'').replace(re,'$1 $2 $3 $4')
document.write('<div>' + x + '<\/div>');

</script>

[this will do from somewhere in 1999 to about 2289]
 
L

Lee

Evertjan. said:
Thomas 'PointedEars' Lahn wrote on 26 okt 2007 in comp.lang.javascript:


Since unix time wants to be written in a special format
like: 1 193 433 406
<http://en.wikipedia.org/wiki/Unix_time>,

I've been working with Unix since about 408400000, and outside
of that Wikipedia article, I've never seen it written in any
special format. It makes it easier to read, though.


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Fri, 26 Oct 2007 21:19:41, Evertjan. <[email protected]>
posted:

Since unix time wants to be written in a special format
like: 1 193 433 406
<http://en.wikipedia.org/wiki/Unix_time>,

I don't see anything there indicating anything other than the common
breaking of numbers into three-digit sub-fields for legibility. It's
not even used throughout the article.

On that page, the time at top right is presumably intended to confuse;
the True Time was 2001-09-09 Sun 01:46:40 GMT.
var re = /^(\d)(\d{3})(\d{3})(\d{3})\d{3}$/
var x = (+new Date()+'').replace(re,'$1 $2 $3 $4')
document.write('<div>' + x + '<\/div>');
[this will do from somewhere in 1999 to about 2289]

2001-09-09 Sun 01:46:40 GMT to 2286-11-20 Sat 17:46:40 GMT.

Using a leading /d+ will do until 33658-09-27 Fri 01:46:39 GMT.

For a wider range, adapt the "comma" routines in js-maths.htm.

Note : 2007-12-31 is special in VBS DatePart "ww" - answer is wrong.
 
E

Evertjan.

Dr J R Stockton wrote on 27 okt 2007 in comp.lang.javascript:
In comp.lang.javascript message <Xns99D5ED4E34A5Eeejj99@ 194.109.133.242>
, Fri, 26 Oct 2007 21:19:41, Evertjan. <[email protected]>
posted:

Since unix time wants to be written in a special format
like: 1 193 433 406
<http://en.wikipedia.org/wiki/Unix_time>,

I don't see anything there indicating anything other than the common
breaking of numbers into three-digit sub-fields for legibility. It's
not even used throughout the article.

On that page, the time at top right is presumably intended to confuse;
the True Time was 2001-09-09 Sun 01:46:40 GMT.
var re = /^(\d)(\d{3})(\d{3})(\d{3})\d{3}$/
var x = (+new Date()+'').replace(re,'$1 $2 $3 $4')
document.write('<div>' + x + '<\/div>');
[this will do from somewhere in 1999 to about 2289]

2001-09-09 Sun 01:46:40 GMT to 2286-11-20 Sat 17:46:40 GMT.

Using a leading /d+ will do until 33658-09-27 Fri 01:46:39 GMT.

Not necessary,
by that time [2286] ECMA surely will have changed the javascript rules.
 

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,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top