c# - Converting a string pulled from the internet into a DateTime
问题描述
I am making a program that scrapes the internet and populates a class with all of the information I'm scraping for. I have a DateTime
property that needs to be populated from the string I am pulling in. The string that comes in looks like this :9:53a ET 08/13/18
.
I have tried using DateTime.ParseExact()
to try and convert this string into a DateTime, using the following format string: "h:mmt ET mm/dd/yy"
So the call looks like this: var lastTime = DateTime.ParseExact(listValue[0].InnerText, format, provider);
Where InnerText
is just the text that I am trying to convert, Format
is the string I specified above, and the provider
is en-US
.
I get the following exception when trying to execute this line: String was not recognized as a valid DateTime.
How would I format this string to convert it to a DateTime correctly?
解决方案
Calling
DateTime.ParseExact("9:53a ET 08/13/18".ToUpper(), "h:mmt ET MM/dd/yy", null)
Returns a correctly parsed date. a
is not recognized as a AM/PM identifier because it is lowercase (hence the toUpper
), and mm/dd/yy
should be MM/dd/yy
since mm
(lowercase is minutes.
推荐阅读
- python - 优化 Python 脚本以在 pandas DataFrame 上进行 200K 行迭代
- r - 我无法使用 merge() 或 left_join() 在 R 中正确合并两个数据帧
- django-rest-framework - 如何在嵌套邮递员请求中传递图像?
- sql-server - 加载由 || 分隔的平面文件时的问题 通过 SSIS 进入 SQL Server 数据库
- kubernetes - k8s:在相同部署的 pod 之间进行通信
- python - PyTorch 线性回归模型
- vbscript - 如何使用VBScript获取2个字符之间的字符串
- laravel - Laravel - 当数字达到一定水平时发送电子邮件
- node.js - 如果 localField 是一个数组,如何使用 $lookup?
- mysql - 是否有可能将查询中的 MySql 表名替换为另一个查询的结果