年,月和日参数描述了波斯日历中无法表示的日期时间 [英] Year, Month, and Day parameters describe an un-representable DateTime in Persian calender

查看:81
本文介绍了年,月和日参数描述了波斯日历中无法表示的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来表示今天的波斯日期...我工作的站点自两个月前以来一直在线,并且工作正常,但今天我收到以下错误消息:

I used the following code in order to present today's Persian date... the site I worked on have been online since two month ago and it have worked fine but today I received the following error:

年,月和日"参数描述了无法表示的日期时间.

Year, Month, and Day parameters describe an un-representable DateTime.

我不知道为什么会这样...有什么办法可以解决这个问题?

and I have no idea why this happened...is there any way to fix this?

protected void lbnChangeDate_Click(object sender, EventArgs e)
{

    PersianCalendar p = new PersianCalendar();
    DateTime date = DateTime.Now;
    int year = p.GetYear(date);
    int month = p.GetMonth(date);
    int day = p.GetDayOfMonth(date);
    DateTime d1 = new DateTime(year, month, day);
    MultiView6.SetActiveView(View12);
    txtDate.Text = DateTime.Parse(d1.ToString()).ToString("yyyy/MM/dd");

}

推荐答案

PersianCalendar 实例获取3个 int ,然后在其构造函数中使用它们.构造函数(暗含)假定您从公历开始过去了数年,数月和数天.

DateTimes don't remember how they were created. So, e.g. d1 doesn't remember that, at some point in the past, you used a PersianCalendar instance to obtain 3 ints and then use those in its constructor. That constructor (implicitly) assumed that you were passing years, months and days from the Gregorian calendar.

然后, DateTime.Parse 不知道很久以前,您访问过 PersianCalendar 并对其进行了不当操作.

Then, further, DateTime.Parse has no knowledge that long ago you accessed a PersianCalendar and did inappropriate things with it.

如果要将波斯日历日期显示为字符串,只需将其用作:

If you want to show the Persian calendar date as a string, just use it as:

protected void lbnChangeDate_Click(object sender, EventArgs e)
{

    PersianCalendar p = new PersianCalendar();
    DateTime date = DateTime.Now;

    txtDate.Text = string.Format("{0:0000}/{1:00}/{2:00}",
                    p.GetYear(date),
                    p.GetMonth(date),
                    p.GetDayOfMonth(date));

}

这篇关于年,月和日参数描述了波斯日历中无法表示的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆