js get current time
时间: 2023-01-11 10:55:36 浏览: 68
To get the current time in JavaScript, you can use the `Date` object and its `getTime()` method. This method returns the number of milliseconds since the Unix epoch (January 1, 1970).
Here is an example of how you can use it:
```
const currentTime = new Date().getTime();
```
If you want to format the time in a specific way, you can use the `toString()` method or one of the many libraries available for formatting dates in JavaScript.
For example, to get the current time in the format "hh:mm:ss", you can do the following:
```
const currentTime = new Date();
const timeString = currentTime.toString();
const formattedTime = timeString.slice(16, 24);
```
This will give you a string like "10:45:30" for the current time. You can also use template literals to make the formatting more concise:
```
const currentTime = new Date();
const formattedTime = `${currentTime.getHours()}:${currentTime.getMinutes()}:${currentTime.getSeconds()}`;
```
There are many other methods available on the `Date` object that allow you to get specific parts of the date and time, such as the year, month, day, etc. You can find more information about these methods in the documentation for the `Date` object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date