For example, 当Regional Options中日期设置:

Date format:yyyy-MM-dd

Time设置:

Time format: tt hh:mm:ss

AM symbol: 上午

PM symbol:下午

Console.WriteLine(DateTime.Now.ToString());

输出结果:2008-9-22 下午 02:11:56

很显然,如果直接将该值存入数据库中,会得到类似异常:Syntax error converting datetime from character string.

如果直接转换该字符串,如DateTime.Prase("2008-9-22 下午 02:11:56"),这段代码如果当前区域日期时间格式更改,将导致String was not recognized as a valid DateTime.

解决方法:通过DateTime.ToString(string format)方法,使用指定格式format将此实例的值转换成其等效的字符串表示。

));

此时,DateTime的输出格式由format参数控制,与Regional Options中的Date/Time的设置无关。

相关文章:

  • 2022-12-23
  • 2021-06-05
  • 2022-12-23
  • 2021-11-26
  • 2021-09-19
  • 2021-09-22
  • 2021-06-26
  • 2021-08-08
猜你喜欢
  • 2022-12-23
  • 2022-03-03
  • 2021-09-03
  • 2021-09-23
相关资源
相似解决方案