yyyy-MM-dd HH:mm:ss 转为时间戳
时间: 2023-07-22 14:04:59 浏览: 90
时间戳是指从1970年1月1日 00:00:00 UTC(协调世界时)开始经过的秒数。要将 yyyy-MM-dd HH:mm:ss 格式的时间转换为时间戳,您可以使用编程语言中的时间函数或库。以下是一个示例使用Python的方法:
```python
import time
from datetime import datetime
time_str = 'yyyy-MM-dd HH:mm:ss'
timestamp = int(datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S').timestamp())
print(timestamp)
```
请将 `yyyy-MM-dd HH:mm:ss` 替换为您要转换的具体时间。这段代码将打印出相应的时间戳。
相关问题
java yyyy-MM-dd HH:mm:ss 转为时间戳
在Java中,您可以使用`java.time`包中的类来进行日期时间和时间戳的转换。以下是一个示例:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class TimestampConverter {
public static void main(String[] args) {
String dateString = "yyyy-MM-dd HH:mm:ss";
String dateTimeString = "2022-12-31 23:59:59";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateString);
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC);
System.out.println(timestamp);
}
}
```
请将"yyyy-MM-dd HH:mm:ss"替换为您要转换的实际日期时间字符串。运行此代码将打印出相应的时间戳。
注意:这里假设您想要获取的是以UTC(协调世界时)为基准的时间戳。如果您需要使用其他时区,请相应调整代码中的`ZoneOffset.UTC`部分。
JS时间戳转为yyyy-mm-dd hh:mm:ss
可以使用以下代码将 JavaScript 时间戳转换为 yyyy-mm-dd hh:mm:ss 格式的日期和时间:
```javascript
function timestampToDatetime(timestamp) {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
const formattedDatetime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return formattedDatetime;
}
const timestamp = 1623342000000; // 示例时间戳
const formattedDatetime = timestampToDatetime(timestamp);
console.log(formattedDatetime);
```
请将 `timestamp` 替换为你要转换的实际时间戳。这段代码将输出格式为 `yyyy-mm-dd hh:mm:ss` 的日期和时间字符串。