I have 3 integer values and I'm trying to combine them to create a datetime variable. I'm trying to do this as I'm needing the user to specify the year through a datetimepicker and then in an array I need the date to start at the first day of the first month of that year.
Currently I have,
int b = 1;
int m = 1;
int y = dateTimePicker1.Value.Year;
DateTime newdate = new DateTime(b, m, y);
I've tried a whole range of different ways of combing the integers together to form 1/1/2017. I know that the integers are holding the correct values when the error appears, but the newdate value is 01/01/0001 12:00:00:AM.
I don't know why the integer y is being changed from 2017 to 0001?
As a result the error message is, Year, Month and Day parameters describe an
un-representable DateTime.
First you need to change parameter order
DateTime newdate = new DateTime(year, month, day);
Second you can use DateTime.Parse or DateTime.TryParse to get date object from string
You have the parameters in the wrong order.
The order is year, month, day:
var newDate = new DateTime(y, m, b);
You are trying to create the date 2017/1/1 which is not valid.
MSDN page
Related
Im new to C# and I am making a program that saves different times. And I wonder how can I write out a single Date with three textBox strings (by typing in the value for day, month and year seperately) and how can I do the same for Time (write the hours and minutes).
int a = int.Parse(textBox_DDVzleta.Text);
int b = int.Parse(textBox_MMVzleta.Text);
int c = int.Parse(textbox_YYVzleta.Text);
DateTime Vzlet = new DateTime(a, b, c);
I tried converting it into a integer but it just doesnt work.
I think you passes your parameters with wrong position.
You use DateTime(year, month, day) constructor but you pass them as DateTime(day, month, year) to this constructor.
Just change your parameter positions like;
DateTime Vzlet = new DateTime(c, b, a);
how can I do the same for Time (write the hours and minutes).
If you mean TimeSpan with Time, you can use it's TimeSpan(hour, minute, second) constructor as well.
TimeSpan ts = new TimeSpan(hour, minute, 0);
There is a so called DateTime Constructor (MSDN Reference). You can initialize this with the given year, month and day integer and it creates for you the datetime object.
DateTime date1 = new DateTime(2010, 8, 18);
In this case you could do
DateTime date1 = new DateTime(int.Parse(textBox_DDVzleta.Text), int.Parse(textBox_MMVzleta.Text), int.Parse(textbox_YYVzleta.Text));
See Screenshot below for the result.
Update
I have also tried your way with converting to int first.
int a = int.Parse("30");
int b = int.Parse("01");
int c = int.Parse("2016");
DateTime date2 = new DateTime(c, b, a); // new DateTime(year, month, day)
Console.WriteLine(date2.ToString());
the result:
You can do this by using string type variable and concatenation.
example:
string x = a.tostring()+b.tostring()+c.tostring();
for spaces between values You can write it as following.
string x = a.tostring() + " " + b.tostring() + " " + c.tostring();
How do I use a specific date as an input value?
var experiment1 = WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
Later on, I'll be convering it to a string, but I need it to be input as a DateTime.
public static string GetDisplayString(string city, DateTime date, double temp)
{
}
I'm using DateTime.Today as a placeholder, simply because it works. The thing is, I need it input as a specific day of a month of a year. (I've tried using (10, 10, 10) but it simply gives me a compiler error.
Edit.: I can't believe I did not figure out all I need to do is to add "new". Thanks folks
In (10, 10, 10) do you want the year 10?
Use the constructor that takes in a year, a month and a day. As below.
DateTime dt = new DateTime(2013, 12, 12);
then call your method
var experiment1 = WorkingWithDates.GetDisplayString("London", dt, 45.00);
PS: If you want year 10
DateTime dt = new DateTime(10, 10, 10);
will work. The year will be 0010
WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
you need a DateTime object to be used in place of DateTime.Today, so construct your instance using
DateTime newdate = new DateTime(2013,10,10) // year, month , day
and then
WorkingWithDates.GetDisplayString("London", newdate, 45.00);
I m trying to get date count from month calender on C# .my code like this
leave.Amount = Convert.ToInt32((mclDateRange.SelectionEnd - mclDateRange.SelectionStart).TotalDays.ToString());
I got error like this
Input string was not in a correct format.
TimeSpan.TotalDays property is of type double, you can get the integer part like:
leave.Amount = (int) (mclDateRange.SelectionEnd - mclDateRange.SelectionStart).TotalDays;
Consider the following example:
double d = 123.22d;
int number = Convert.ToInt32(d.ToString());
The would result into the exception
Input string was not in a correct format.
So in your code, you can leave out the call ToString and it would be fine, like:
leave.Amount =
Convert.ToInt32(
(mclDateRange.SelectionEnd - mclDateRange.SelectionStart).TotalDays);
Here’s a step by step example on how to diff two datetime objects. Just apply this to your code
DateTime startDate = DateTime.Parse("01/01/2013");
DateTime endDate = DateTime.Parse("05/22/2013");
TimeSpan dateDiff = endDate.Subtract(startDate);
int dayDiff = dateDiff.Days;
If you want to round fractional days (like 4 days 18 hrs) days to the nearest one (5 in this case) then use TotalDays property and convert to Int.
This is the way i did for datetimepicker hope it will work to monthcalender
DateTime dt;
DateTime Todate ,FromDate;
Todate = DateTime.ParseExact(datetimepicker1.Value.Date.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture);
FromDate = DateTime.ParseExact(datetimepicker2.Value.Date.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture);
double datedifference = (Todate - FromDate).TotalDays;
Then can check as date checking like this
if(datedifference <2)
{
something ..........
}
I have problem in finding the date using day of the week.
For example : i have past date lets say,
Date date= Convert.TodateTime("01/08/2013");
08th Jan 2013 th Day of the week is Tuesday.
Now i want current week's tuesday's date. How i can do it.
Note : The past date is dynamic. It will change in every loop.
You can use the enumeration DayOfWeek
The DayOfWeek enumeration represents the day of the week in calendars
that have seven days per week. The value of the constants in this
enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If
cast to an integer, its value ranges from zero (which indicates
DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).
We can use the conversion to integer to calculate the difference from the current date of the same week day
DateTime dtOld = new DateTime(2013,1,8);
int num = (int)dtOld.DayOfWeek;
int num2 = (int)DateTime.Today.DayOfWeek;
DateTime result = DateTime.Today.AddDays(num - num2);
This also seems appropriate to create an extension method
public static class DateTimeExtensions
{
public static DateTime EquivalentWeekDay(this DateTime dtOld)
{
int num = (int)dtOld.DayOfWeek;
int num2 = (int)DateTime.Today.DayOfWeek;
return DateTime.Today.AddDays(num - num2);
}
}
and now you could call it with
DateTime weekDay = Convert.ToDateTime("01/08/2013").EquivalentWeekDay();
I may be a bit late to the party, but my solution is very similar:
DateTime.Today.AddDays(-(int)(DateTime.Today.DayOfWeek - DayOfWeek.Tuesday));
This will get the Tuesday of the current week, where finding Tuesday is the primary goal (I may have misunderstood the question).
You can use this....
public static void Main()
{
//current date
DateTime dt = DateTime.UtcNow.AddHours(6);
//you can use it custom date
var cmYear = new DateTime(dt.Year, dt.Month, dt.Day);
//here 2 is the day value of the week in a date
var customDateWeek = cmYear.AddDays(-2);
Console.WriteLine(dt);
Console.WriteLine(cmYear);
Console.WriteLine("Date: " + customDateWeek);
Console.WriteLine();
Console.ReadKey();
}
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.