简体   繁体   中英

Return Datetime.Day in persian culture by c#

I intend to return the day of the month in Persian culture . I have already set the culture and it works well. the day which shows is in Persian calendar. But I need to check somewhere the exact day of the month,

for example today is 2018-2-3 and in Persian calendar is 1396-11-14,

var date = DateTime.Now 

shows me the right Datetime in Persian calendar. But

var date = DateTime.Now.Day 

shows me 3 rather than 14. I need 14! I know I can change it to string and get the day but it make my code so dirty and I am not so interested in this way, because I used this in many parts of the program. Is there any way ?

I want to clarify a few things.

A DateTime instance is always on Gregorian calendar no matter what. Suprisingly, it has a few constructors that takes Calendar as a parameter but I don't think this parameter effect anything about keeping it as a Gregorian equivalent internally.

If you wanna get some parts of a DateTime in a specific calendar, you need to call that DateTime instance as a parameter of GetXXX methods on that calendar instance.

For examle;

var today = DateTime.Today;
var persian = new PersianCalendar();
Console.WriteLine(persian.GetDayOfMonth(today)); // 14

Here a demonstration .

you will try this code,.

 CultureInfo info = new CultureInfo("fa-Ir");
 info.DateTimeFormat.Calendar = new PersianCalendar();          
 string day= "";
 Thread.CurrentThread.CurrentCulture = info;
 day= DateTime.Now.ToString("dd");

refer this link: https://archive.codeplex.com/?p=persianculture

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM