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;
Related
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.
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.
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.
I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way?
DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
if dateTimeWycenaPortfelaObliczDataOd is of type DateTime, You can use:
dateTimeWycenaPortfelaObliczDataOd.Date
to get the date part only (time will be 00:00:00...).
If you want to get the very last tick of the date, you can use:
dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)
but you really better work with the next date (.AddDays(1)).
In any case, there is no need to convert to string and back to DateTime.
DateTime objects have a Date property which might be what you need.
You can use the following properties / methods on a DateTime object to get your values :
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);
It would help to know why you're needing it, but this would work.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);
Using the Date attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.
You could use the Date property of the DateTime object to accomplish what you need.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);
If you really want it to end at 23:59:59 you can do:
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);
Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 it would now be 2010-03-06 00:00:00.
Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);
DateTime varObliczDo = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59);
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
You could work with TimeSpan:
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);
Like that you avoid at least the parsing, which can fail depending on the local culture settings.
How do you change the day part of a DateTime structure?
I have a DateTime with the value of 3/31/2009 8:00:00 AM and would like to change it to any other date but the time should be same. I don't need a solution to add day(s), I need to replace it with any arbitrary date.
How do I achieve this?
To construct a DateTime from two other DateTimes, one of which contains the date and one of which contains the time, you can use:
DateTime result = date.Date + time.TimeOfDay;
day = 1
month = 4
year = 2009
Date2 = new DateTime(day,month,year,Date1.Hours,Date1.Minute,Date1.Seconds)
s = s.AddDays(1)
You should use DateTime.AddDays method. Like this:
DateTime s2 = s.AddDays( 1 );