将字符串转换为datetime [英] Convert string into datetime
本文介绍了将字符串转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问如何将DateTime.Now.AddDays(-1)之类的字符串转换为日期时间?
例如。我在App.Config中有一行代码如下:
May i ask how to convert string like "DateTime.Now.AddDays(-1)" into datetime?
Eg. i have a line of code in App.Config like this:
<add key="EndDate" value="DateTime.Now.AddDays(-1)"/>
然后我在我的程序中调用EndDate,如下所示:
then i call the EndDate in my program like this:
EndDate = Convert.ToString(ConfigurationManager.AppSettings["EndDate"]);
如何转换上面的EndDate?
how can i convert the EndDate above?
推荐答案
那个不行。 App.Config中的所有值都是字符串。我会在app.config中存储相对日期。例如,只存储-1。然后,当您读入值时,您将-1添加到Now。不要试图将整个事物存储为字符串。
That won't work. All values in App.Config are string. I would instead store the relative date in app.config. For example, store just -1. Then when you read in the value you would add -1 to Now. Don't try to store the whole thing as a string.
这篇关于将字符串转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文