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);
}
Related
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;
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");
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 );
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.
How can I convert a number between 1 and 7 into a DateTime object in C# which represents the day of the week? The numbers are coming from a XML file which I am parsing. I am retrieving each instance of a field containing a number between 1 and 7 which represents a day of the week between Sunday and Saturday.
I would assume casting to a DayOfWeek object would give you a day of the week
DayOfWeek day = (DayOfWeek)myInt;
As far as a DateTime object goes, the object represents a specific day, not necessarily a random day of the week. You may try adding a # of days to a specific date if this is what you're trying to achieve.
http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx
In order to get a DateTime, you'd need a specific range of dates that you want the weekday to fall under (since a DateTime is a specific date and time, and a weekday isn't).
There is a DayOfWeek enumeration (whose values actually range from 0-6). If all you need is something to represent the day of the week, then you should be able to cast your int to a DayOfWeek like..
DayOfWeek myDay = (DayOfWeek)yourInt;
If you need an actual DateTime, you'll need a start date. You could then do...
DateTime myDate = startDate.AddDays(
(int)startDate.DayOfWeek >= yourInt ?
(int)startDate.DayOfWeek - yourInt :
(int)startDate.DayOfWeek - yourInt + 7);
This will give you a DateTime for the next occuring instance of the day of the week you're describing.
DayOfWeek.Sunday is zero, so you could start with an arbitrary fixed date that you know to be Sunday, and add a value between 0 and 6:
public DateTime GetDayOfWeek(int dayOfWeek)
{
if (dayOfWeek < 0 || dayOfWeek > 6) throw new ArgumentOutOfRangeException(...);
// 4 January 2009 was a Sunday
return new DateTime(2009,1,4).AddDays(dayOfWeek);
}
I'm not sure why you would want this though.
If you only want it to get a localized version of the day of the week as in:
GetDayOfWeek(3).ToString("dddd"); // Gets name of day of week for current culture
an alternative would be to use DateTimeFormatInfo.DayNames or DateTimeFormatInfo.AbbreviatedDayNames for the culture you want.
A DateTime instance represents alway a complete date and cannot only represent a day of the week. If the actual date does not matter, take any monday (assuming 0 represents monday) and just add the number of the day.
Int32 dayOfWeek = 3;
// date represents a thursday since 2009/04/20 is a monday
DateTime date = new DateTime(2009, 04, 20).AddDays(dayOfWeek);
Else I agree with Adam Robinson's answer - if you just want to hold the day of a week, stick with the DayOfWeek enum (zero is sunday) instead of using an integer.