Home  >  Article  >  Web Front-end  >  Does javascript call system time function?

Does javascript call system time function?

PHPz
PHPzOriginal
2023-05-16 10:59:37410browse

Yes, JavaScript has a built-in Date object that can be used to get the current system time. Here are a few examples:

  1. Get the current date and time:
var currentDate = new Date();
console.log(currentDate);

Output:

Tue Jun 15 2021 09:57:31 GMT+0800 (China Standard Time)
  1. Get the current date:
var currentDate = new Date();
var year = currentDate.getFullYear();
var month = currentDate.getMonth() + 1;
var day = currentDate.getDate();
console.log(year + "-" + month + "-" + day);

Output:

2021-6-15
  1. Get the current time:
var currentDate = new Date();
var hours = currentDate.getHours();
var minutes = currentDate.getMinutes();
var seconds = currentDate.getSeconds();
console.log(hours + ":" + minutes + ":" + seconds);

Output:

9:57:31

It should be noted that the Date object returns The time is based on the local time of the user's computer. If you need to get the server time, you need to send an AJAX request to the server and get the time in its response.

The above is the detailed content of Does javascript call system time function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn