在不在本地主机中的服务器中发生日期时间解析错误,可能是由于本地主机和服务器上的时区不同,代码:我正在尝试将24小时时间格式转换为12小时(使用am和PM)
string timesx2 = hr2[0]+":" + hr2[1]; // 19:22
string s2 = DateTime.ParseExact(timesx2, "HHmm", CultureInfo.CurrentCulture)
.ToString("hh:mm tt"); // output in localhost is: 7.22 PM 发布于 2017-07-27 13:43:33
你应该使用固定的区域性(当然,如果你不需要转换成你的时区)
string timesx2 =hr2[0] + ":" + hr2[1]; // 19:22
string s2 = DateTime.ParseExact(timesx2, "HH:mm", CultureInfo.InvariantCulture).ToString("hh:mm tt", CultureInfo.InvariantCulture); // output in localhost is: 7.22 PM 在印度文化里也没问题。
发布于 2017-07-27 14:11:15
您的分析字符串缺少冒号。
当您尝试解析由HHmm组成的字符串时,您合成的时间字符串的格式为HH:mm。这是行不通的。
另外,如果您希望出现个位数小时数,请从输出格式字符串中删除第二个h。否则,输出将为07:22 PM
string timesx2 = hr2[0]+":" + hr2[1]; // 19:22
string s2 = DateTime.ParseExact(timesx2, "HH:mm", CultureInfo.InvariantCulture)
.ToString("h:mm tt"); // output in localhost is: 7:22 PM 发布于 2017-07-27 14:26:53
大写字母"H“表示24小时制时间,小写字母"h”表示12小时制时间,并考虑候选字符串中的AM/PM。
DateTime.ParseExact("3/21/2015 8:56:04 AM", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture)https://stackoverflow.com/questions/45341899
复制相似问题