Azzurro94
Azzurro94

Reputation: 499

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 ?

Upvotes: 0

Views: 744

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98868

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.

Upvotes: 3

Angappan.S
Angappan.S

Reputation: 90

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

Upvotes: 0

Related Questions