解析中文日期时间

huangapple go评论74阅读模式
英文:

Parse Chinese DateTime

问题

我尝试将以下字符串转换为DateTimeOffset:

二 5月 16 14:41:40 +0800 2023

它的翻译是“Tue May 16 14:41:40 +0800 2023”。

我尝试了以下代码:

DateTimeOffset.Parse(lastLogin, new CultureInfo("zh-CN"), DateTimeStyles.None)

但很遗憾,没有成功。

我还尝试了ParseExact(),使用了以下格式:ddd MMM d HH:mm:ss zzz yyyyd MMM d HH:mm:ss zzz yyyy。同样没有成功。

System.FormatException: '未将字符串 '二 5月 16 14:43:10 +0800 2023' 识别为有效的 DateTime。索引 '0' 处有一个未知单词。'

英文:

I'm trying to convert the following string to a DateTimeOffset

二 5月 16 14:41:40 +0800 2023

which translates to "Tue May 16 14:41:40 +0800 2023"

I have tried the following code:

DateTimeOffset.Parse(lastLogin, new CultureInfo("zh-CN"), DateTimeStyles.None)

But unfortunately without success.

I have also tried ParseExact() with the following formats: ddd MMM d HH:mm:ss zzz yyyy and d MMM d HH:mm:ss zzz yyyy. Also without success.

> System.FormatException: 'The string '二 5月 16 14:43:10 +0800 2023' was not recognized as a valid DateTime. There is an unknown word starting at index '0'.'

答案1

得分: 3

The default zh-CN culture has the abbreviated day names as the following array:

"周日", "周一", "周二", "周三", "周四", "周五", "周六"

So you could create your own DateTimeFormatInfo object you can use to parse the string. You can use the zh-CN culture as the starting point:

using System.Globalization;

var zhcnCulture = CultureInfo.GetCultureInfo("zh-CN");
var writeableClone = (DateTimeFormatInfo)zhcnCulture.DateTimeFormat.Clone();
writeableClone.AbbreviatedDayNames = new string[] { "日", "一", "二", "三", "四", "五", "六" };
// or use:
// writeableClone.AbbreviatedDayNames = writeableClone.ShortestDayNames;

var dateTxt = "二 5月 16 14:41:40 +0800 2023";
var format = "ddd M月 d HH:mm:ss zzz yyyy";
var parsedDate = DateTimeOffset.ParseExact(dateTxt, format, writeableClone);

You need to clone the starting point because the starting point is a readonly object that will give an error if you try to change it.

英文:

The default zh-CN culture has the abbreviated day names as the following array:

"周日", "周一", "周二", "周三", "周四", "周五", "周六"

So you could create your own DateTimeFormatInfo object you can use to parse the string. You can use the zh-CN culture as the starting point:

using System.Globalization;

var zhcnCulture = CultureInfo.GetCultureInfo("zh-CN");
var writeableClone = (DateTimeFormatInfo)zhcnCulture.DateTimeFormat.Clone();
writeableClone.AbbreviatedDayNames = new string[] { "日", "一", "二", "三", "四", "五", "六" };
// or use:
// writeableClone.AbbreviatedDayNames = writeableClone.ShortestDayNames;

var dateTxt = "二 5月 16 14:41:40 +0800 2023";
var format = "ddd M月 d HH:mm:ss zzz yyyy";
var parsedDate = DateTimeOffset.ParseExact(dateTxt, format, writeableClone);

You need to clone the starting point because the starting point is a readonly object that will give an error if you try to change it.

答案2

得分: 1

Google Translate 表示第一个字符只是“two”,而不是星期二,没有星期的提示。如果我进一步从英语开始翻译成中文,星期二看起来是这样的:周二

如果我把这个新字符作为输入的一部分,解析成功。

因此我建议你的输入字符串某种方式缺少一个字符。

英文:

Google Translate says the first glyph is just "two", not Tuesday, with no hint of day-of-week. If I further start from English and translate to Chinese, Tuesday looks like this: 周二.

If I then take the new glyph and use it as part of the input, the parse succeeds.

Therefore I suggest your input string is somehow missing a character.

huangapple
  • 本文由 发表于 2023年5月17日 21:46:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定