string转时间戳
时间: 2023-07-20 14:23:14 浏览: 51
可以使用以下代码将一个字符串类型的时间转换成时间戳:
```python
import time
# 将字符串时间转换为时间数组
time_array = time.strptime("2021-05-01 10:00:00", "%Y-%m-%d %H:%M:%S")
# 将时间数组转换为时间戳
timestamp = int(time.mktime(time_array))
print(timestamp)
```
其中,第一个参数是字符串时间,第二个参数是字符串时间的格式,`time.strptime()` 方法可以将字符串时间转换成时间数组,`time.mktime()` 方法可以将时间数组转换成时间戳。
相关问题
string 转时间戳 c++ 毫秒
在C语言中,将字符串转换成时间戳(以毫秒为单位)有两种常见的方法。
方法一:使用time.h库中的函数
time.h库提供了将字符串转换为时间戳的函数,包括strptime和mktime。其中,strptime函数可以将格式化字符串转换为tm结构体,然后使用mktime将其转换为时间戳。
示例代码:
```
#include <stdio.h>
#include <time.h>
int main()
{
char str[] = "2022-02-05 13:14:15.678";
struct tm tm_time;
strptime(str, "%Y-%m-%d %H:%M:%S.", &tm_time); // 解析毫秒部分
int milliseconds = 678;
time_t timestamp = mktime(&tm_time) * 1000 + milliseconds; // 转化为毫秒
printf("Timestamp: %ld\n", timestamp);
return 0;
}
```
方法二:使用第三方库
仅仅使用time.h库需要手动解析字符串中的每个部分,比较繁琐。因此,还有其他第三方解析库可以使用,例如libtime、zonedatetime、strptime_s等等。
示例代码:
```
#include <stdio.h>
#include <zonedatetime.h>
int main()
{
char str[] = "2022-02-05 13:14:15.678";
struct tm tm_time;
zonedatetime_parse(str, &tm_time); // 解析
int milliseconds = 678;
time_t timestamp = zonedatetime_to_timestamp(&tm_time) * 1000 + milliseconds; // 转化为毫秒
printf("Timestamp: %ld\n", timestamp);
return 0;
}
```
总之,无论使用哪种方法,都可以将字符串转换为时间戳(以毫秒为单位)。需要根据实际情况选择适合自己的方法。
string时间戳转date
要将String类型的时间戳转换为Date,你可以使用DateFormat或DateTimeFormatter来实现。
方法一:使用DateFormat
你可以使用DateFormat及其子类SimpleDateFormat来进行格式化和解析日期。首先,你需要创建一个SimpleDateFormat对象,并指定你想要的日期格式,比如"yyyy-MM-dd HH:mm:ss"。然后,使用该对象的parse方法将String类型的时间戳转换为Date类型。以下是一个示例代码:
```
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse("2018-06-16 10:00:00");```
其中,"2018-06-16 10:00:00"是你要转换的String类型的时间戳。
方法二:使用DateTimeFormatter(JDK8以上)
如果你使用的是JDK8及以上版本,你可以使用DateTimeFormatter来代替SimpleDateFormat。DateTimeFormatter是线程安全的。首先,你需要创建一个DateTimeFormatter对象,并指定你想要的日期格式,比如"yyyy-MM-dd HH:mm:ss"。然后,使用该对象的parse方法将String类型的时间戳转换为LocalDate类型。以下是一个示例代码:
```
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate date = LocalDate.parse("2018-06-16 10:00:00", formatter);
```
其中,"2018-06-16 10:00:00"是你要转换的String类型的时间戳。
在以上两种方法中,你可以根据你的具体需求选择适合的方法来进行String类型时间戳的转换。同时,记得根据你所使用的类库的版本来选择合适的方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)