I want to make this 1332251639632 to this 1332251639
I try this code, but since is not a string it dosent work
var date = new Date();
var t = date.getTime();
var p = t.substring(10);
alert(p);
I want to cut it since in php time() return 10 digit number
I have the opinion that a better approach is:
var dateObject = new Date(),
time = dateObject.getTime();
Math.floor ( time / 1000 );
Now the reasons are:
parseInt()ECMA Specs expects a string to be converted by a certain radix. Behind the scene the interpreter is working with strings and at the end returns integer number. Example MDN how the function is intended to work. As Chris Wesseling points the it is slower because of the additional work with the string and radix. ES5 which will be implemented in the future versions of the browsers, will impose the usage of radix, here is why:
parseInt( "011" ); //returns 9, 0 starting string is indicating octal number
parseInt( "011", 10 ); //returns 11, as expected
getTime()MDN will return a number, milliseconds since 1 January 1970 00:00:00 UTC, there is no convertion from string to number. This means "semantically" is better to use rounding function.
A little off-topic Even the Linux y2k(38) problem won't make any difference because the number is 64-bit, and the integer in Javascript is presented in 53-bitsECMA Specs SO question.
Like this?
var p = parseInt(t/1000);
parseInt
compared to using Math.floor
. See my answer - Chris Wesseling 2012-04-05 16:11
You could do it with rounding as suggested Ilia's solution, or with substring like this:
var date = new Date();
var t = date.getTime().toString();
var p = t.substring(0, 10);
alert(p);
alert ( +new Date() );
Bakudan 2012-04-25 08:11
Try:
var t = "" + date.getTime();
var p = t.substring(0,10);
t.substring(0,10);
Travis J 2012-04-05 01:02
String(…)
also abhor seeing var a = new Array()
instead of an empty array literal. Go figure - RobG 2012-04-05 02:16
"" +
conversion is perfectly re-usable. It is your variable names which should reflect the type of the variable, not what would be assumed. Stop making assumptions and your code will get a lot better - Travis J 2012-04-05 03:32
How about this?
var date = new Date();
var t = date.getTime();
var p = parseInt(t.toString().match(/\d{10}/));
alert(p);
It converts the number into a string, matches the first 10 digits, then reconverts the result into number.
Curious whether there was any difference between
var p = parseInt(t/1000);
and
var p = Math.floor(t/1000);
I did this:
start = new Date().getTime(); for ( var i=0; i<1000000; i++) parseInt(start/1000); new Date().getTime() - start;
And
start = new Date().getTime(); for ( var i=0; i<1000000; i++) Math.floor(start/1000); new Date().getTime() - start;
In Chromium parseInt
would take about 7.2 seconds against Math.floor
6.6 on my netbook.
Firefox complained about the script taking to long. It could only do 200000 operations in about the same time.
So I guess this is very implementation dependent.
parsInt
on aNumber
have any (dis)advantages over just usingMath.floor(t/1000)
- Chris Wesseling 2012-04-05 12:22