如何在Python中将字符串转换为日期时间

时间:2018-10-09 04:52:37

标签: python datetime odoo-11

我已串联

date(date field)
time(float)
date_time_to(Char)

我正在以"2018-10-09 20.00"格式输出。

我需要使用'%m/%d/%Y %H:%M:%S'格式的输出

我使用了"obj.date_time_to = datetime.strptime(self.date_time_to, "%m/%d/%Y %H:%M:%S.%f")"

我正在

ValueError: time data '2018-10-09 20.00' does not match format '%m/%d/%Y %H:%M:%S.%f'

2 个答案:

答案 0 :(得分:1)

您需要使用正确的格式来解析字符串:

$FolderList = Get-ChildItem -Directory
foreach ($folder in $FolderList)
{
    set-location $folder.FullName
    $size = Get-ChildItem -Recurse | Measure-Object -Sum Length
    $info = $folder.FullName + "    FileCount: " + $size.Count.ToString() + "   Size: " + [math]::Round(($size.Sum / 1GB),4).ToString() + " GB"
    write-host $info
}

答案 1 :(得分:1)

您可以使用datetime模块。查看Python文档。

例如:

import datetime

x = "09/10/2018"
date = datetime.datetime.strptime(date, "%d/%M/%Y") # string to datetime