Hijri and Gregorian DateTime constructor - c#

what is the correct behavior for the Calendar objected passed to the constructor of DateTime type?
I have the components year, month and day as the below example:
day = 1
month = 5
year = 1433 (which is the current Hijri year)
when creating a datetime object using the below code the result is a valid Greg Date
HijriCalendar hijri = new HijriCalendar();
//Get the First Day in the Month
DateTime firstDayInMonth = new DateTime(1433, month, 1, hijri);
while using the below code generates a valid Hijri date:
GregorianCalendar greg = new GregorianCalendar();
//Get the First Day in the Month
DateTime firstDayInMonth = new DateTime(1433, month, 1, greg);
is that a correct result?

Your first example is correct. The DateTime will not be in the Hijri format, it will just be the standardised equivalent of what you gave it. See the following code for how to get the Hirji date:
HijriCalendar hijri = new HijriCalendar();
DateTime firstDayInMonth = new DateTime(1433, 10, 11, hijri);
Console.WriteLine(hijri.GetEra(firstDayInMonth)); // 1
Console.WriteLine(hijri.GetYear(firstDayInMonth)); // 1433
Console.WriteLine(hijri.GetMonth(firstDayInMonth)); // 10
Console.WriteLine(hijri.GetDayOfMonth(firstDayInMonth)); // 11
Your second block of code was just setting the gregorian date "1/1/1433" so when you were inspecting it you weren't getting a hirji date, you were just getting the date you gave it in the 15th century.
Looking at http://msdn.microsoft.com/en-us/library/system.globalization.hijricalendar.aspx and seeing the methods there should give you a better idea of what you should be doing on the calendar object and what should happen on the DateTime object.

You've not actually asked a meaningful question. If you're trying to convert a given date from one calender to another then more than the date will change, after all the Hijri calender has different months to the gregorian.
Check out this site for examples - it even has downloadable code.

Related

Is there any correct converter for Hijri dates to Gregorian dates

I have work on many projects with date converts. for example, I work on the solar calendar and how to convert them to Gregorian dates and vice versa. The solar calendar (Persian calendar) is almost similar to the Gregorian date in terms of the number of days in a year[leap or not].
But I recently worked on a project with the lunar calendar. As I research on the Lunar calendar, I realized that there isn't any single logical method to convert the lunar system to solar(at least as far as I know).
Here are some references links that I researched on them:
Cannot convert from Hijri Date to Gregorian date (c#)
Convert date from Hijri Calendar to Gregorian Calendar and vise
versa
Hijri Date To gregorian using DateTime.Parse
Convert Hijri date to Gregorian dat
As I followed the above links, training, and testing people presented algorithms, I noticed that none of them are absolutely correct.
Suffice it to say, just Convert "1441/02/30" [Safar 30th] to Gregorian date which every method that you want to try.
The following image is helpful for the aforementioned example.
I put here my some test codes and fails:
CultureInfo arSA = new CultureInfo("ar-SA");
arSA.DateTimeFormat.Calendar = new HijriCalendar();
var dateValue = DateTime.ParseExact("1441/02/30", "yyyy/MM/dd", arSA);
return dateValue.ToString();
The error for above Code:
The DateTime represented by the string is not supported in calendar System.Globalization.HijriCalendar.
HijriCalendar hc = new HijriCalendar();
DateTime date = new DateTime(1441, 2, 30, hc);
return date.ToString();
The error for above Code:
"Day must be between 1 and 29 for month 2."
string hijri = "1441/2/30";
HijriCalendar hc = new HijriCalendar();
int year = int.Parse(hijri.Substring(0, 4));
string rem = hijri.Remove(0, 5);
int end = rem.IndexOf('/', 0);
int month = int.Parse(rem.Substring(0, end));
rem = rem.Remove(0, end + 1);
int day = int.Parse(rem);
DateTime date = new DateTime(year, month, day, hc);
return date.ToShortDateString();
The error for above Code:
"Day must be between 1 and 29 for month 2."
After that, I was trying to understand that is there any algorithm to deploying the convert Hijri date to Gregorian date?
So I test some Islamic online date converter and I got surprised!
Just try to enter "1441/2/30" on both date convertors.
the first one returning October 30, 2019
And the second one returning 29 October 2019
Hijri gregorian-converter
Islamic Date Converter - Gregorian Calendar Converter
So I don't know is there any real algorithm to convert Hijri dates to Gregorian .
Thanks in advance for your time.
For more info,
https://www.wikiwand.com/simple/Islamic_calendar
Update: I know there isn't any correct library for know. but if someone has knowledge or details about Hijri Calendar (all its interface) please just describe here I really want to deploy a Hijri calendar for all.
You can use something like this code to convert and parse hijri calendar strings to gregorian DateTime.
//assuming current culture is like en-us or something with gregorian calendar
string hijri = "1441/2/30";
HijriCalendar hc = new HijriCalendar();
var dateParts = hijri.Split('/');
DateTime? gregorianDate=null;
if (dateParts.Length == 3 && int.TryParse(dateParts[0], out int year) &&
int.TryParse(dateParts[1], out int month) && int.TryParse(dateParts[2], out int day))
{
if(month == 2 && day==30)
{
var temp = hc.ToDateTime(year, month, 29, 0, 0, 0, 0);
gregorianDate = temp.AddDays(1);
}
else
{
gregorianDate = hc.ToDateTime(year, month, day, 0, 0, 0, 0);
}
}

How to set default month and day in the datetimepicker?

I have searched online and i only managed to find codes to set the year month and day.
dateTimePicker2.Value = new DateTime(2017,12,31);
I tried using the custom format and it does not seem to work
dateTimePicker2.CustomFormat = "DD/MM";
dateTimePicker2.Value = new DateTime(12,31);
You cannot create DateTime object only from day and month. DateTime simply doesn't have this kind of constructor. DateTime Constructors
So you need to go with some kind of "workaround"
- Use "dummy" year and when you need to use a date - use only Month and Day properties.
var dummyYear = 2000;
dateTimePicker2.Value = new DateTime(dummyYear, 12, 31);
Another workaround will be to use ParseExact method which will create DateTime based on the format you are using "dd/MM"
var date = DateTime.ParseExact("31/12", "dd/MM", CultureInfo.InvariantCulture);
dateTimePicker2.Value = date; // 12/31/2017
Notice that when you did not provide a year - current year will be used.
Another notice: DD is invalid format for days it should be lower case "dd"
You cannot partially set the date without a year, it's not valid.
What you cand do is specify the month and date in code as "default" values, and get the current year programmtically (or whatever year you want), and use that value for the year.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM";
dateTimePicker1.Value = DateTime.Now;

Return day Name in c#

I want to return the name of day like Saturday or Monday
I used this code :
DateTime date = new DateTime(DateTime.Now.Date.Day);
MessageBox.Show(date.DayOfWeek.ToString());
But it doesn't work it return the name of day but doesn't correct day
and when i change the date in my computer it still return the same day
Rather try something like
MessageBox.Show(DateTime.Today.DayOfWeek.ToString());
DateTime.Today Property
Gets the current date.
Your problem is that
DateTime date = new DateTime(DateTime.Now.Date.Day);
evaluates to
{01/Jan/0001 12:00:00 AM}
The constructor you used was DateTime Constructor (Int64)
Initializes a new instance of the DateTime structure to a specified
number of ticks.
This line:
DateTime date = new DateTime(DateTime.Now.Date.Day)
Should be:
DateTime date = new DateTime(DateTime.Now)
You are putting the day in a date variable which will probably be in the year 1900.

Setting A particular Day in a date

I am using Calender Extender Control in the AjaxControlToolkit. There are basically 2 controls of date : Start Date and End date (both associated with calender extender). Based on start Date selected, I populate date in the end date field like adding no of months or days. But like I have been able to add months, but also wants to set a particular day of that month which I am unable to do.
Example:
Today date is 18 Dec 2012. Something like 1st of every three months, So I add 3 months the month comes out to be Feb 2013. But I want to set Day 1st Feb 2013. I am unable to do it. Kindly help.
You can set whatever day of month by add month.
DateTime todayDate = DateTime.Now;
DateTime after3MonthDate = todayDate.AddMonths(3);
//Set First Day of Month
after3MonthDate = new DateTime(after3MonthDate.Year, after3MonthDate.Month, 1);
This code can be used for existing date time variable to set the day part to the first day of the month:
if(myDate.Day > 1)
{
myDate = myDate.AddDays(-(myDate.Day - 1));
}
Try this:
// Here is the simple wrapper method to get the first day of the month:
public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
// Set the due date...
DueDate.Text = (FirstDayOfMonthFromDateTime(DateTime.Parse(StartDate.Text).AddMonths(N))).ToShortDateString();
You can also modify the wrapper method to get any day of the month:
public DateTime DayOfMonthFromDateTime(DateTime dateTime, int day)
{
return new DateTime(dateTime.Year, dateTime.Month, day);
}

How do you convert an int representing days-from-zero to DateTime?

I have an int representing a number of Gregorian days from Year Zero (thanks, Erlang). How do I convert this to a DateTime object? I can't create a DateTime(0,0,0), and Convert.DateTime(int) throws an invalid cast.
If you have a number, and you know the date that it represents (from Erlang), you can calculate the offset from any date you choose. Preferred is a base date in the zone that the results will be in, this will minimize calender conversion effects. (The Gregorian calendar is valid from about 1600).
If you know that offset, you can use the choosen date as the base for future calculations.
Example:
I want my offset date to be: 1/1/2000. This will be the date that I calculcate from.
I know number 37892 from erlang is actually 1/1/1970 (this is an example).
Then I can calculate the offset:
var myBaseDate = new DateTime(2000,1,1);
var exampleNrOfDays = 37892;
var exampleDate = new DateTime(1970,1,1);
var offset = exampleDate - myBaseDate;
var offsetInDays = exampleNrOfDays - (int)offset.TotalDays;
// Now I can calculate
var daysFromErlang = 30000; // <= example
var theDate = myBaseDate.AddDays(daysFromErlang - offsetInDays);
This shows how to calculate number of days from a given date. http://dotnetperls.com/datetime-elapsed
if day zero is 0/0/0 then it is 365+30+1 day before DateTime.Min which is 1/1/1. So you can subtract days from year zero by 365+30+1 and add to DateTime.Min
Now Month 1 is January which is 31 days but what is Month 0? I assumed it is 30 days.
With 0, you probably mean 0:00 on the 1st of January, year 1. There is no year 0 in the gregorian calendar as far as i know.
If the above is right, you can just do
DateTime date = new DateTime();
date.AddDays(numberOfDays);
because the default constructor 'DateTime()' returns the "zero" DateTime object.
See the DateTime reference for more informations.
I am not sure if you are aware of this, but there is a Calendar object in System.Globalization. Not only that but there is a GregorianCalendar object as well.
so try this:
GregorianCalendar calendar = new GregorianCalendar();
DateTime minSupportedDateTime = calendar.MinSupportedDateTime;
//which is the first moment of January 1, 0001 C.E.
DateTime myDate = minSupportedDateTime.AddDays(55000);
//this is when you add the number of days you have.
Thanks,
Bleepzter
PS. Don't forget to mark my answer if it has helped you solve your problem! Thanks.

Categories

Resources