简体   繁体   English

如何将日期从 26-MAY-20 03.43.48.861000000 重新格式化为 'yyyy-mm-dd hh:mm:ss'?

[英]How to reformat date from 26-MAY-20 03.43.48.861000000 to 'yyyy-mm-dd hh:mm:ss'?

How would I convert '26-MAY-20 03.43.48.861000000' into 'yyyy-mm-dd hh:mm:ss' using datetime in Python?如何使用datetime中的日期时间将'26-MAY-20 03.43.48.861000000'转换为'yyyy-mm-dd hh:mm:ss'

So big picture, the goal is to create a date object out of the first string, and then format the date into the second string.这么大的图,目标是从第一个字符串中创建一个日期 object,然后将日期格式化为第二个字符串。

datetime.strptime will be your best friend here, along with datetime.strftime . datetime.strptime将与datetime.strftime一起成为您最好的朋友。

By referencing the library's format codes , we can put together a format of the first string.通过引用库的格式代码,我们可以将第一个字符串的格式放在一起。

from datetime import datetime

# I don't know why your datetime doesn't conform to the 1989 C standard but I'm just truncating it
may_26 = datetime.strptime('26-MAY-20 03.43.48.861000000'[:-3], "%d-%b-%y %H.%M.%S.%f")

output = datetime.strftime(may_26, "%Y-%m-%d %H:%M:%S")

I am making a few assumptions for you in the formatting, like assuming your datetime is on 24hr format, and stuff, so definitely check for yourself.我在格式上为你做了一些假设,比如假设你的日期时间是 24 小时格式,等等,所以一定要自己检查。

pandas.to_datetime would also parse the nanoseconds (I've modified the timestring to have some...): pandas.to_datetime也会解析纳秒(我已经修改了时间字符串以具有一些......):

import pandas as pd
t = pd.to_datetime('26-MAY-20 03.43.48.861000100', format="%d-%b-%y %H.%M.%S.%f")

t
Out[41]: Timestamp('2020-05-26 03:43:48.861000100')

t.microsecond
Out[42]: 861000

t.nanosecond
Out[43]: 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 csv 文件中的时间格式从 DD:MM:YY HH:MM 更改为 YYYY-MM-DD HH:MM:SS。 或 YYYY/MM/DD HH:MM:SS - How to change time format in a csv file from DD:MM:YY HH:MM to YYYY-MM-DD HH:MM:SS. or YYYY/MM/DD HH:MM:SS 使用 python 将日期格式 mm/dd/yyyy hh:mm:ss AM 更改为 yyyy-mm-dd hh:mm:ss 格式 - change date format mm/dd/yyyy hh:mm:ss AM to yyyy-mm-dd hh:mm:ss format using python 在YYYY-MM-DD中转换日期时间HH:MM [:SS [.SSSSSS]] - Transform datetime in YYYY-MM-DD HH:MM[:SS[.SSSSSS]] 在 python 中将日期从 DD/MM/YYYY HH.mm.ss 转换为 MM-DD-YYYY HH:MM:SS 时出错 - Error converting date from DD/MM/YYYY HH.mm.ss to MM-DD-YYYY HH:MM:SS in python Python:如何将 UTC 时间戳从 mm/dd/yyyy hh:mm:ss AM 更改为 dd/mm/yyyy hh:mm:ss AM - Python: How to change a UTC timestamp from mm/dd/yyyy hh:mm:ss AM to dd/mm/yyyy hh:mm:ss AM Python正则表达式日期YYYY-MM-DD HH:MM:SS - Python Regular Expression Date YYYY-MM-DD HH:MM:SS 将带时区的日期格式更改为 yyyy-mm-dd hh:mm:ss - change date format with timezone to yyyy-mm-dd hh:mm:ss 将字符串 MM-dd-yyyy hh:mm:ss 转换为 PySpark 中的时间戳 yyyy-MM-dd hh:mm:ss - Convert string MM-dd-yyyy hh:mm:ss to timestamp yyyy-MM-dd hh:mm:ss in PySpark 怎么把yyyy-mm-dd hh:mm:ss转换成UNIX时间戳? - How to convert yyyy-mm-dd hh:mm:ss to UNIX timestamp? 如何睡到特定时间 YYYY-MM-DD HH:MM:SS? - How to sleep until a specific time YYYY-MM-DD HH:MM:SS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM