本文介绍了DateTime.ParseExact用于非传统岁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取使用一个单一的格式字符串的格式如下日期:

I'm trying to read dates in the following format using a single format string:

"1000.12.31"
"999.12.31"
"99.12.31"

这将对应,日期在多年的公元1000年,公元999和AD 99

These would correspond to dates in the years 1000 AD, 999 AD and 99 AD.

我试过下面的格式字符串:

I've tried the following format strings:

yyyy.Md 。这个失败的999年和99。

yyyy.M.d. This fails for the years 999 and 99.

yy.M.d 。这将失败,1000和999这也解释99在1999年。

yy.M.d. This fails for 1000 and 999. It also interprets 99 as 1999.

我要诉诸手动解析它(它是足够简单),但我很好奇,如果这样的事情,甚至有可能与 DateTime.ParseExact ,或者与其他内置的方法?

I'm about to resort to parsing it manually (it's simple enough for that), but i'm curious if something like this is even possible with DateTime.ParseExact, or perhaps with another built-in method?

推荐答案

这是微不足道的手动解析;我不认为这是值得努力尝试去适应它 DateTime.ParseExact 工作

This is trivial to parse manually; I don’t think it’s worth the effort trying to adapt it to work with DateTime.ParseExact.

string str = "99.12.31";
int[] parts = str.Split('.').Select(int.Parse).ToArray();
DateTime date = new DateTime(parts[0], parts[1], parts[2]);

这篇关于DateTime.ParseExact用于非传统岁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:42