js获取当前时间 格式为 "yyyy-MM-dd HH:mm:ss"的字符串
时间: 2023-07-22 22:19:58 浏览: 67
可以使用 JavaScript 的 Date 对象和字符串格式化函数来获取当前时间并格式化为 "yyyy-MM-dd HH:mm:ss" 的字符串。具体代码如下:
```
function formatDate(date) {
// 将日期格式化为 "yyyy-MM-dd HH:mm:ss" 的字符串
let year = date.getFullYear();
let month = padZero(date.getMonth() + 1);
let day = padZero(date.getDate());
let hours = padZero(date.getHours());
let minutes = padZero(date.getMinutes());
let seconds = padZero(date.getSeconds());
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function padZero(value) {
// 将数值补零,保证输出的位数一致
return value.toString().padStart(2, '0');
}
let now = new Date();
let formattedDate = formatDate(now);
console.log(formattedDate);
```
输出的结果类似于 "2021-05-24 14:32:45"。
相关问题
js获取当前时间格式为(YYYY-MM-DD 24hh:mm:ss
要获取当前时间并格式化为"YYYY-MM-DD 24hh:mm:ss"的格式,可以使用JavaScript的Date对象和相关方法来实现。下面是一种实现方式:
```javascript
// 创建一个Date对象
var currentDate = new Date();
// 获取年、月、日、小时、分钟和秒
var year = currentDate.getFullYear();
var month = ('0' + (currentDate.getMonth() + 1)).slice(-2);
var day = ('0' + currentDate.getDate()).slice(-2);
var hours = ('0' + currentDate.getHours()).slice(-2);
var minutes = ('0' + currentDate.getMinutes()).slice(-2);
var seconds = ('0' + currentDate.getSeconds()).slice(-2);
// 格式化为指定的字符串格式
var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
console.log(formattedDate); // 输出格式化后的时间字符串
```
这段代码首先创建了一个Date对象,然后使用Date对象的方法获取当前的年、月、日、小时、分钟和秒。接着使用字符串拼接的方式将这些值组合成指定的格式,并将结果打印出来。
java获取当前日期 格式为yyyy-MM-dd HH:mm:ss
你使用Java的`SimpleDateFormat`类来获取当前日期并按照指定格式进行格式化。以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 创建日期格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前日期时间
Date currentDate = new Date();
// 格式化日期时间
String formattedDate = sdf.format(currentDate);
// 输出结果
System.out.println("当前日期时间: " + formattedDate);
}
}
```
这段代码中,我们创建了一个`SimpleDateFormat`对象,并指定日期格式为"yyyy-MM-dd HH:mm:ss"。然后,通过`new Date()`获取当前日期时间,再使用`format()`方法将其格式化为指定格式的字符串。最后,将格式化后的日期时间打印出来。
运行代码后,你将得到类似以下的输出结果:
```
当前日期时间: 2022-03-15 10:30:45
```
请注意,输出结果将根据你运行代码的时间而有所不同。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)