如何将 C# 中的 DateTime.now 转换为 yyyy-mm-dd hh:mm:ss.sssssss?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10348952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:29:46  来源:igfitidea点击:

How I can convert DateTime.now in C# to yyyy-mm-dd hh:mm:ss.sssssss?

c#datetime

提问by George Panayi

I'm storing in my database using a store procedure the date time stamp using a function called sysdatetime()and a type of varchar(max)which stores in the database in this format yyyy-mm-dd hh:mm:ss.sssssss. When I'm creating a new post its fine but when I'm trying to update the record I have to send a parameter to the store procedure with the current datetime. I have tried this one

我使用存储过程将日期时间戳存储在我的数据库中,该日期时间戳使用一个被调用的函数sysdatetime()和一种varchar(max)以这种格式存储在数据库中的类型yyyy-mm-dd hh:mm:ss.sssssss。当我创建一个新帖子时很好,但是当我尝试更新记录时,我必须使用当前的datetime. 我试过这个

DateTime.Now.ToString(yyyy-mm-dd hh:mm:ss.sssssss)

but I'm getting an error. what i have to do?

但我遇到了一个错误。我该怎么办?

P.s: see the image below with the error message

Ps:见下图,错误信息

enter image description here

在此处输入图片说明

采纳答案by Jon Skeet

I suspect if you really need the string representation, you actuallywant:

我怀疑如果你真的需要字符串表示,你实际上想要:

string text = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fffffff", 
                                       CultureInfo.InvariantCulture)

Note that:

注意:

  • Unless you reallywant the local time, use UtcNowinstead of Now. For timestamps, you almost always want UTC.
  • I doubt that you want to use the time separator of the current culture, hence the specification of CultureInfo.InvariantCulture
  • "MM" means months whereas "mm" means minutes
  • "HH" means 24-hour clock whereas "hh" means 12-hour clock
  • "ff..." is used for fractions of a second
  • 除非您真的想要当地时间,否则请使用UtcNow代替Now。对于时间戳,您几乎总是需要 UTC。
  • 我怀疑你想使用当前文化的时间分隔符,因此规范 CultureInfo.InvariantCulture
  • “MM”表示月,而“mm”表示分钟
  • “HH”表示 24 小时制,“hh”表示 12 小时制
  • “ff...”用于几分之一秒

See MSDNfor more details of custom date and time formatting.

有关自定义日期和时间格式的更多详细信息,请参阅MSDN

However, I'd stronglyrecommend that you try to avoid string conversions wherever possible. Why can't your stored procedure just use the relevant DateTimetype instead of a string representation? Then you can pass the value to the stored procedure asa DateTime(via a command parameter) and you can get rid of the error-prone string conversion clutter.

但是,我强烈建议您尽可能避免字符串转换。为什么您的存储过程不能只使用相关DateTime类型而不是字符串表示?然后您可以将值传递给存储过程作为一个DateTime(通过命令参数),你可以摆脱容易出错的字符串转换的混乱的。

回答by Dima

Use this code:

使用此代码:

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")

See this ref: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

请参阅此参考:http: //msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

回答by MUG4N

Here a small sample:

这里有一个小例子:

DateTime date3 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Date with milliseconds: {0:MM/dd/yyy hh:mm:ss.fff}", 
                  date3);
// Displays the following output to the console:
//       Date with milliseconds: 01/01/2008 12:30:45.125 

Just put the appropriate number of "f"s into your command and you are done

只需将适当数量的“f”放入您的命令中即可完成