Get date from calendar control asp.net - c#

How can i get the current date, day, month from the calendar individually on page load function in C# like the following:
day :Friday
date: 13 // just the d, i don't the full format
month: August

Generally if you are using a Calendar control, you should be able to access a property like SelectedDate that would give you a proper DateTime object, which you could use to pull the properties that you need :
// Get the selected date
var date = YourCalendar.SelectedDate;
// Get the day of week (e.g. Friday)
var dayOfWeek = date.ToString("dddd");
// Get the day of the month (e.g. 19); Use date.ToString("d") if you need a string
var day = date.Day;
// Get the full month name (e.g. August)
var month = date.ToString("MMMM");

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;

Calculate Last Day of the Next Month

I am attempting to use the DateTime function in C# to calculate the last day of next month.
For example, today is December 17th 2015. I want the DateTime function to return January 31st 2016 (the last day of next month).
I am using the following to calculate the first day of next month (this works):
DateTime firstDayNextMonth = DateTime.Today.AddDays(-DateTime.Now.Day+1).AddMonths(1);
DateTime reference = DateTime.Now;
DateTime firstDayThisMonth = new DateTime(reference.Year, reference.Month, 1);
DateTime firstDayPlusTwoMonths = firstDayThisMonth.AddMonths(2);
DateTime lastDayNextMonth = firstDayPlusTwoMonths.AddDays(-1);
DateTime endOfLastDayNextMonth = firstDayPlusTwoMonths.AddTicks(-1);
Demo: http://rextester.com/AKDI52378
//system date or any date u want this case it is a calendar picker - 22/03/2016
DateTime today = dtpFrom.Value;
//Add a month to your date example , it now becomes - 22/04/2016
DateTime endOfMonth = new DateTime(today.Year, today.Month,today.Day).AddMonths(1);
//Get the last date off the above which is - 30
int getlastday = DateTime.DaysInMonth(endOfMonth.Year, endOfMonth.Month);
//Now set the date to the value which will be the last day off the next month - 30/04/2016
DateTime newDate = new DateTime(endOfMonth.Year, endOfMonth.Month, getlastday);
DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month);
var lastDayInNextMonth = DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month );
# Ben : DateTime.Now.AddMonths(1) will add 1 month to the current date not substract 11 months.
DateTime.Now.AddMonths(1).Year will give 2016 not 2015 refer the attached image
try this:
int Day= DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month+1>12 ? 01 : DateTime.Now.Month+1 );

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);
}

Hijri and Gregorian DateTime constructor

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.

Categories

Resources