3

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'.'

Steven
  • 166,672
  • 24
  • 332
  • 435
Bart
  • 75
  • 6

2 Answers2

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.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
1

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks for your quick answer Joel. This string comes from the 'lastlog' command on a Chinese Ubuntu Linux server. So it could be that the DateTime is formatted wrongly in Linux. I'm not a Chinese speaker myself, so i'm not sure what the correct way of writing dates is in Chinese. – Bart May 17 '23 at 14:01
  • @Bart Since the day-of-week is redundant (implied by day/month/year), and you're parsing a log file where the date format should be consistent, I suggest always pre-trimming that first element before calling `Parse()` or `ParseExact()` – Joel Coehoorn May 17 '23 at 14:03