简体   繁体   English

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

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

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 . 我使用一个名为sysdatetime()的函数和一个varchar(max)函数在存储过程中使用存储过程存储日期时间戳,该函数以这种格式存储在数据库中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 . 当我正在创建一个新帖子时它很好,但是当我尝试更新记录时,我必须使用当前datetime向存储过程发送参数。 I have tried this one 我试过这个

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

but I'm getting an error. 但我收到了一个错误。 what i have to do? 我该怎么办?

Ps: see the image below with the error message Ps:请参阅下面的图片并显示错误消息

在此输入图像描述

I suspect if you really need the string representation, you actually want: 我怀疑你是否真的需要字符串表示,你真的想要:

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

Note that: 注意:

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

See MSDN for more details of custom date and time formatting. 有关自定义日期和时间格式的详细信息,请参阅MSDN

However, I'd strongly recommend that you try to avoid string conversions wherever possible. 但是,我强烈建议您尽可能避免字符串转换。 Why can't your stored procedure just use the relevant DateTime type instead of a string representation? 为什么您的存储过程不能使用相关的DateTime类型而不是字符串表示? Then you can pass the value to the stored procedure as a DateTime (via a command parameter) and you can get rid of the error-prone string conversion clutter. 然后,您可以将值作为 DateTime传递给存储过程(通过命令参数),您可以摆脱容易出错的字符串转换混乱。

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 请参阅此ref: http//msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

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”即可完成

暂无
暂无

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

相关问题 如何在 C# 中将“YYYY-MM-DDThh:mmTZD”转换为 yyyy-MM-dd hh:mm:ss - How to convert “YYYY-MM-DDThh:mmTZD” to yyyy-MM-dd hh:mm:ss in C# 将字符串转换为日期时间,格式为 yyyy-MM-dd HH:mm:ss in C# - convert string to datetime with form yyyy-MM-dd HH:mm:ss in C# 如何通过linq将MM / dd / YYYY hh:mm:ss AM转换为YYYY-MM-dd datetime格式? - how to convert MM/dd/YYYY hh:mm:ss AM to YYYY-MM-dd datetime format by linq? 在 c# LINQ 中比较日期时间“MM/dd/yyyy hh:mm:ss”和“yyyy-MM-dd hh:mm:ss” - Compare dateTime "MM/dd/yyyy hh:mm:ss" with "yyyy-MM-dd hh:mm:ss" in c# LINQ 我需要yyyy-mm-dd hh:mm:SQL Server中的ss在C#中成为yyyy-mm-dd - I need yyyy-mm-dd hh:mm:ss in SQL Server to become yyyy-mm-dd in C# 如何将字符串“dd.MM.yyyy HH:mm:ss.mmm”转换为 c# 中的日期时间 - how to convert string “dd.MM.yyyy HH:mm:ss.mmm” to the datetime in c# C#将字符串“yyyy-MM-dd hh:mm:ss 'UTC'”转换为日期时间 - C# Convert a string "yyyy-MM-dd hh:mm:ss 'UTC'" to date time 将dd / MM / yyyy hh:mm:ss.fff从String转换为C#中的DateTime - Convert dd/MM/yyyy hh:mm:ss.fff from String to DateTime in C# 如何将 Paypal 的 HH:MM:SS DD Mmm(.) YYYY PST/PDT 转换为 C# UTC 日期时间? - How do I convert Paypal's HH:MM:SS DD Mmm(.) YYYY PST/PDT to a C# UTC DateTime? 使用以下格式转换datetime字符串:(yyyy-MM-dd'T'hh:mm:ss-zzz) - Convert datetime string with this format: (yyyy-MM-dd'T'hh:mm:ss-zzz)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM