C# 为什么默认系统日期时间到PersianCalendar之间的转换不起作用?
我使用以下代码行将日期时间存储在数据库中:C# 为什么默认系统日期时间到PersianCalendar之间的转换不起作用?,c#,datetime,model-view-controller,globalization,C#,Datetime,Model View Controller,Globalization,我使用以下代码行将日期时间存储在数据库中: var now=DateTime.Now; 所以在存储之后,数据库中的数据是这样的,并且在调用实体之后: 2019-01-26 17:27:46.297 当我尝试使用此方法将其转换为另一个区域性datetime时: public static string ToPersianDate(this DateTime t) { var pc = new PersianCalendar(); return $"{pc.G
var now=DateTime.Now;
所以在存储之后,数据库中的数据是这样的,并且在调用实体之后:
2019-01-26 17:27:46.297
当我尝试使用此方法将其转换为另一个区域性datetime时:
public static string ToPersianDate(this DateTime t)
{
var pc = new PersianCalendar();
return $"{pc.GetYear(t)}/{pc.GetMonth(t)}/{pc.GetDayOfMonth(t)}";
}
使用此方法后:
var persianDate=now.ToPersianDate();
因此,我得到以下字符串:
2019/01/26
但我希望得到这样的结果:
1397/11/06
下面是一个将时间转换为PersianCalendar的完整示例
using System;
using System.Globalization;
namespace ConsoleApp1
{
public class Program
{
public static void Main(String[] args)
{
var date = DateTime.Now;
var str = date.ToPersianDate();
Console.WriteLine(str);
Console.Read();
}
}
public static class Extender
{
public static string ToPersianDate(this DateTime t)
{
var pc = new PersianCalendar();
return $"{pc.GetYear(t)}/{pc.GetMonth(t)}/{pc.GetDayOfMonth(t)}";
}
}
}
您的问题不可再现,代码附在后供进一步参考@ZDaneshi,请提供一个示例,我无法再现您的问题,它正在工作fine@hessamhedieh我编辑了我的例子