How to measure execution time of javascript code with callbacks

I have a piece of javascript code that I am executing using the node.js interpreter. for(var i = 1; i < LIMIT; i++){ db.users.save({id : i, name : "MongoUser [" + i + "]"}, function(err, saved) { if( err || !saved ) console.log("Error"); else console.log("Saved"); }); } I want to know how to measure the time taken by these db insert operations. I could compute th

如何用回调来测量JavaScript代码的执行时间

我有一段使用node.js解释器执行的JavaScript代码。 for(var i = 1; i < LIMIT; i++){ db.users.save({id : i, name : "MongoUser [" + i + "]"}, function(err, saved) { if( err || !saved ) console.log("Error"); else console.log("Saved"); }); } 我想知道如何测量这些db插入操作所花费的时间。 我可以计算这段代码之前和之后的Date值的差异,但由于代码的异步性质,这可能不正确。 使用

How to convert date to milliseconds by javascript?

This question already has an answer here: How do you get a timestamp in JavaScript? 35 answers One way is to use year, month and day as parameters on new Date new Date(year, month [, day [, hours [, minutes [, seconds [, milliseconds]]]]]); You can prepare your date string by using a function. Note: Month is 0-11, that is why m-1 Here is a snippet: function prepareDate(d) { [d, m

如何将日期转换为毫秒由JavaScript?

这个问题在这里已经有了答案: 你如何获得JavaScript中的时间戳? 35个答案 一种方法是使用年,月和日作为new Date参数 新日期(年,月[,日[,小时[,分钟[,秒[,毫秒]]]]]); 您可以使用函数来准备日期字符串。 注意:月份是0-11,这就是m-1 这是一个片段: function prepareDate(d) { [d, m, y] = d.split("-"); //Split the string return [y, m - 1, d]; //Return as an array with y,m,d sequence }

how to convert particular time into UTC timestamp

This question already has an answer here: How do you get a timestamp in JavaScript? 35 answers How to set Hours,minutes,seconds to Date which is in GMT 3 answers You can just create UTC Timestamps with the full date. You can use the .getTime() function of the Date object. You will get the milliseconds since 1970/01/01 and then divide it with 1000 to get the seconds. let datestring = "20

如何将特定时间转换为UTC时间戳

这个问题在这里已经有了答案: 你如何获得JavaScript中的时间戳? 35个答案 如何设置小时,分钟,秒到格林威治标准时间3天的答案 您可以使用完整日期创建UTC时间戳。 您可以使用Date对象的.getTime()函数。 你会得到自1970/01/01以来的毫秒数,然后将它除以1000以获得秒数。 let datestring = "2017-10-21 13:22:01"; let date = new Date(datestring); console.log(date.getTime() / 1000); // will return 15085849

Get timestamp of current hour in Javascript

This question already has an answer here: How do you get a timestamp in JavaScript? 35 answers 一个很好运作: var UTCTime= (new Date()).toUTCString(); var d = new Date(); var UTCHour = d.getUTCHours(); 喜欢这个?

在Javascript中获取当前小时的时间戳

这个问题在这里已经有了答案: 你如何获得JavaScript中的时间戳? 35个答案 一个很好运作: var UTCTime= (new Date()).toUTCString(); var d = new Date(); var UTCHour = d.getUTCHours(); 喜欢这个?

Timestamp from the web browser?

Possible Duplicate: How can I determine a web user's time zone? How do you get a timestamp in JavaScript? I would like to get the timestamp from my user web browser. Example: If now a user comes to my website from USA and another comes from Spain, I would like to save the respective time and date that it is now in USA and Spain. 只需使用new Date().toString() ; Getting the time with

Web浏览器的时间戳?

可能重复: 我怎样才能确定一个网络用户的时区? 你如何获得JavaScript中的时间戳? 我想从我的用户网页浏览器获取时间戳。 示例:如果现在用户从美国来到我的网站,而另一个用户从西班牙来到我的网站,我想节省它现在在美国和西班牙的相应时间和日期。 只需使用new Date().toString() ; 通过JavaCcript获取时间可返回当前用户的机器时间。 以下JavaScript代码将返回1970年1月1日午夜以来的毫秒数。 var timestamp =

Javascript method to find number of milliseconds from 1970

This question already has an answer here: How do you get a timestamp in JavaScript? 35 answers 使用getTime函数: var dt = new Date('2012-01-01'); var time = dt.getTime(); You can get it with: new Date('2012-01-01').getTime(); The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.

Javascript方法从1970年开始查找毫秒数

这个问题在这里已经有了答案: 你如何获得JavaScript中的时间戳? 35个答案 使用getTime函数: var dt = new Date('2012-01-01'); var time = dt.getTime(); 你可以得到它: new Date('2012-01-01').getTime(); getTime()方法返回1970年1月1日午夜到指定日期之间的毫秒数。

Get time in javascript like python time.time()

This question already has an answer here: How do you get a timestamp in JavaScript? 35 answers 像这样: var time = +new Date; You can use time = (new Date()).getTime() / 1000; See getTime() on the MDN.

用python time.time()获取时间

这个问题在这里已经有了答案: 你如何获得JavaScript中的时间戳? 35个答案 像这样: var time = +new Date; 您可以使用 time = (new Date()).getTime() / 1000; 请参阅MDN上的getTime()。

How to get time in milliseconds since the unix epoch in Javascript?

Possible Duplicate: How do you get a timestamp in JavaScript? Calculating milliseconds from epoch How can I get the current epoch time in Javascript? Basically the number of milliseconds since midnight, 1970-01-01. var milliseconds = (new Date).getTime(); new Date().valueOf() will do the trick. )

如何获得自从JavaScript中的unix时代以毫秒为单位的时间?

可能重复: 你如何获得JavaScript中的时间戳? 从纪元计算毫秒 我如何在Javascript中获得当前的epoch时间? 基本上是自1970-01-01午夜以来的毫秒数。 var milliseconds = (new Date).getTime(); new Date().valueOf()将做到这一点。 )

Format number to always show 2 decimal places

I would like to format my numbers to always display 2 decimal places, rounding where applicable. Examples: number display ------ ------- 1 1.00 1.341 1.34 1.345 1.35 I have been using this: parseFloat(num).toFixed(2); But it's displaying 1 as 1 , rather than 1.00 . This works fine in FF4: parseFloat(Math.round(num3 * 100) / 100).toFixed(2); Live Demo var

格式编号始终显示2位小数

我想将我的号码格式化为始终显示2位小数,并在适用的位置舍入。 例子: number display ------ ------- 1 1.00 1.341 1.34 1.345 1.35 我一直在使用这个: parseFloat(num).toFixed(2); 但它将1显示为1 ,而不是1.00 。 这在FF4中正常工作: parseFloat(Math.round(num3 * 100) / 100).toFixed(2); 现场演示 var num1 = "1"; document.getElementById('num1').innerHTML = parseFloat(Mat

How to print a number with commas as thousands separators in JavaScript

I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this? Here is how I am doing it: function numberWithCommas(x) { x = x.toString(); var pattern = /(-?d+)(d{3})/; while (pattern.test(x)) x = x.replace(pattern, "$1,$2"); return x; } Is th

如何在JavaScript中使用逗号作为千位分隔符打印数字

我正在尝试使用逗号作为数千个分隔符在JavaScript中打印整数。 例如,我想将编号1234567显示为“1,234,567”。 我会如何去做这件事? 这是我如何做到的: function numberWithCommas(x) { x = x.toString(); var pattern = /(-?d+)(d{3})/; while (pattern.test(x)) x = x.replace(pattern, "$1,$2"); return x; } 有没有更简单或更优雅的方式来做到这一点? 如果它也可以与浮动器一起工作,那将是