简体   繁体   English

将DateTime转换为字符串格式yyyy-MM-dd时出错

[英]Error converting DateTime to string format yyyy-MM-dd

I'm trying to convert some DateTime values to string format yyyy-MM-dd . 我正在尝试将一些DateTime值转换为字符串格式yyyy-MM-dd The problem is i only need the date but in my case model.StartDate contains both date and time. 问题是我只需要日期,但在我的情况下, model.StartDate包含日期和时间。 When declaring model.StartDate as string "start" looks like this: 4/1/2014 12:00:00 AM . 将model.StartDate声明为字符串“start”时,如下所示: 4/1/2014 12:00:00 AM I get this error when trying to parse: 我在尝试解析时遇到此错误:

System.FormatException was unhandled by user code Message=String was not recognized as a valid DateTime. 用户代码未处理System.FormatException Message = String未被识别为有效的DateTime。

My best guess is that the error occurs because string contains both Date and Time but i could be wrong. 我最好的猜测是错误发生,因为字符串包含日期和时间,但我可能是错的。 If i explore model.StartDate further i can also find Day, DayOfTheWeek etc. Is this the right approach? 如果我探索model.StartDate,我还可以找到Day,DayOfTheWeek等。这是正确的方法吗? I just want to convert model.StartDate to string "start" with format yyyy-MM-dd . 我只想将model.StartDate转换为字符串“start”,格式为yyyy-MM-dd

Heres my code: 继承我的代码:

string start = model.StartDate.ToString();
model.StartDate = DateTime.ParseExact(start, "yyyy-MM-dd", CultureInfo.InvariantCulture);

string end = model.EndDate.ToString();
model.EndDate = DateTime.ParseExact(end, "yyyy-MM-dd", CultureInfo.InvariantCulture);

Dunno what the problem is, might be that start contains time? 不知道问题是什么,可能是开始包含时间? I have no idea. 我不知道。

The model.StartDate and model.EndDate are DateTime properties from the view model: model.StartDate和model.EndDate是视图模型中的DateTime属性:

        [NopResourceDisplayName("Admin.GAStatistics.GAStatistics.StartDate")]
        [UIHint("DateNullable")]
        public DateTime? StartDate { get; set; }

        [NopResourceDisplayName("Admin.GAStatistics.GAStatistics.EndDate")]
        [UIHint("DateNullable")]
        public DateTime? EndDate { get; set; }

EDIT: Iv'e uploaded a image here showing the actual output i'm getting in the debugger: https://imageshack.com/i/1n51u2p 编辑:我在这里上传了一个图像,显示我在调试器中获得的实际输出: https ://imageshack.com/i/1n51u2p

Thank you 谢谢

You are converting the dates to string but you don't specify the format. 您正在将日期转换为字符串,但未指定格式。 Try 尝试

string start = model.StartDate.ToString("yyyy-MM-dd);

ToString() uses the current thread's Culture format to convert the date to a string, including the time. ToString()使用当前线程的Culture格式将日期转换为字符串,包括时间。 The format used is G , the general date and time format. 使用的格式是G ,一般日期和时间格式。

Just for this format, you don't need to specify CultureInfo.InvariantCulture because there isn't anything culture specific. 仅针对此格式,您无需指定CultureInfo.InvariantCulture,因为没有任何特定于文化的内容。 A common gotcha with the yyyy/MM/dd format though is that some cultures use - as the date specifier, and / is the date placeholder. 然而, yyyy/MM/dd格式的常见问题是某些文化使用-作为日期说明符,而/是日期占位符。 In such a case you would have to use: 在这种情况下,您将不得不使用:

string start = model.StartDate.ToString("yyyy-MM-dd,CultureInfo.InvariantCulture);

UPDATE UPDATE

From the comments, it seems that model.StartDate and model.EndDate are not DateTime objects but strings with a specific format that include a time element. 从注释中,似乎model.StartDatemodel.EndDate 不是 DateTime对象,而是具有包含时间元素的特定格式的字符串。

What you are actually trying to do is parse the original string to a DateTime object, then format this object to the new format string: 您实际上要做的是将原始字符串解析为DateTime对象,然后将此对象格式化为新的格式字符串:

var date=DateTime.ParseExact(model.StartDate,"M/d/YYYY HH:mm:ss tt",
         CultureInfo.InvariantCulture);
model.StartDate=date.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture);

assuming the string the value "4/1/2014 12:00:00 AM" for April 1, 2014 假设字符串为2014年4月1日的值“4/1/2014 12:00:00 AM”

You appear to be misunderstanding how ParseExact works (or actually what it does). 您似乎误解了ParseExact的工作原理(或实际上它的功能)。 Parsing, in general, is the process of taking data of type X and converting it to type Y - in the context of DateTime this means converting a date string to a DateTime instance. 通常,解析是获取类型X的数据并将其转换为类型Y的过程 - 在DateTime的上下文中,这意味着将日期string转换为DateTime实例。 This is completely different to what you are trying to do which is formatting a DateTime instance. 这与您尝试执行的格式化 DateTime实例完全不同。

Given you already have the date you don't need to parse anything, all you need to do is format the date 鉴于您已经拥有了不需要解析任何内容的日期,您只需格式化日期即可

model.StartDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

CultureInfo.InvariantCulture is important when working with fixed formats because you want to make sure you aren't culture aware ie the format you specify is exactly how you want it to display in all cultures. CultureInfo.InvariantCulture在使用固定格式时非常重要,因为您希望确保您不具备文化意识,即您指定的格式正是您希望它在所有文化中显示的格式。

Use the .Date property of a DateTime to get only the Date part. 使用DateTime的.Date属性仅获取Date部分。 Your ToString() will also yield different results based on the current culture meaning that while your ToString() and then TryParse might work for you right now, it will break in other countries. 你的ToString()也会根据当前的文化产生不同的结果,这意味着你的ToString()和TryParse现在可能适合你,它会在其他国家中断。

You can use ToString() overload to specify a specific format. 您可以使用ToString()重载来指定特定格式。 Different formats can be found here 这里可以找到不同的格式

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM