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);
Related
I have to calculate the date or create a date which is 10 days into next month. I have with me month and year. If the month is April and year is 2019 I need a date which is 10th of may 2019.
You can simply use the methods available in the DateTime structure to do math with dates
// As example, replaces it with whatever date you have
DateTime current = new DateTime(2019,4,5);
DateTime next = new DateTime(current.Year, current.Month, 1).AddMonths(1).AddDays(9);
You can use AddMonths() and AddDays() extension method of DateTime.
From MSDN :
AddMonths() : Returns a new DateTime that adds the specified number of
months to the value of this instance.
AddDays() : Returns a new DateTime that adds the specified number of
days to the value of this instance.
//Considered this is your Current date
DateTime existingDate = new DateTime(2019, 4, 1);
//Below code will add +1 month to current month and +9 days to current date.
var result = existingDate.AddMonths(1).AddDays(9);
Output :
CurrentDate : 4/1/2019 12:00:00 AM
Next Date (+1 month and +9 days) :5/10/2019 12:00:00 AM
POC : .net Fiddle
var dateNow = DateTime.Now.AddMonths(1);
var date = new DateTime(dateNow.Year, dateNow.Month, 10);
Try this:
DateTime dt = new DateTime(2019, 04, 01);
DateTime newDT = dt.AddMonths(1).AddDays(9);
I have used like this,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString(), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
I have also tried like this,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString("dd/MM/yyyy"), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
but it is not reflected. I also changed in access database format as Short date, but that is also not gave correct answer.
You can use dueDate.Date
DateTime.Date Property
Gets the date component of this instance.
Example
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
Results
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
//
You can play with a demo here
Try this
DateTime dueDateTime = Convert.ToDateTime(row["Date required"].ToString());
DateTime dueDate= dueDateTime.Date;
First Convert it into Stting and then to DateTime
and then get Date from DateTime
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
I'm having strange behavior of DateTime in c#.
I'm trying to initialize a datepicker in January. So I make a new date:
DateTime MyDate = new DateTime(2017, 0, 15);
But I get this exception:
System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
If I use (2017, 1, 15) it works but the time dialog, initialized with:
DatePickerDialog dialog = new DatePickerDialog(
Activity,
this,
MyDate.Year,
MyDate.Month,
MyDate.Day);
Goes on February.
Well I tried to "cheat" and did:
DateTime MyDate = new DateTime(2017, 1, 15);
DateTime = DateTime.AddMonths(-1);
No error, but the date picker goes on February.
The only way to have January is:
DateTime MyDate = new DateTime(2017, 12, 15);
What am I doing wrong?
DateTime.Month is a value between 1 and 12, which aligns with what most people think a month 'number' is.
Per the android docs, theDatePickerDialog constructor you are calling accepts a zero-based month. It accepts values in the range 0-11, so you need to subtract one from the DateTime.Month.
DatePickerDialog dialog = new DatePickerDialog(Activity,
this, MyDate.Year, MyDate.Month - 1, MyDate.Day);
The issue is how the DateTime object treats month values (1-12) and how the DatePickerDialog treats month values (0-11).
DateTime constructor:
strange behavior of DateTime
DateTime MyDate = new DateTime(2017, 0, 15);
If we take a look at the DateTime constructor, it is clearly stated that the month value should be 1 through 12, which is not valid in your case and hence the exception. We can correct it as below:
DateTime MyDate = new DateTime(2017, 1, 15);
DatePickerDialog constructor:
Exception (or strange behavior) will arise when we try - new DatePickerDialog in combination with the month value of DateTime as the constructor of DatePickerDialog is expecting the month values from 0-11.
It is stated that int: the initially selected month (0-11 for compatibility with MONTH)
The approach which can then be followed is to give the correct index for month to DatePickerDialog constructor as below:
DateTime MyDate = new DateTime(2017, 1, 15);
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
MyDate.Year,
MyDate.Month - 1,
MyDate.Day);
I'm having some trouble here.
Did some research on Google but I can't seem to find what I'm looking for.
I'm trying to ask for two inputs (datetimes) in hh:mm format, subtract one for the other then return the result of that value in minutes.
The problem is that I want to return that value as an integer and I can't seem to find the right way to do it.
In C/C++ I wouldn't have this kind of issues...
Anyways, here's a snippet of what I'm talking about.
private int DuraçaoTreino(DateTime dtInicioTreino, DateTime dtFimTreino, int dtDuraçao)
{
Console.WriteLine("Introduza a hora de inicio (hh:mm): ");
dtInicioTreino = Convert.ToDateTime(Console.Read());
Console.WriteLine("Introduza a hora de fim (hh:mm): ");
dtFimTreino = Convert.ToDateTime(Console.Read());
dtDuraçao = (dtFimTreino - dtInicioTreino); // duração da sessão de treino
dtDuraçao = Convert.ToDecimal(Console.Read());
return dtDuraçao;
}
And that's pretty much it... I'm new to C# so if you see anything wrong please be kind.
Thanks in advance.
What you're talking about is a TimeSpan:
DateTime dtBegin = new DateTime(2011,5,1,22,0,0) ; // 10pm 1 May 2011
DateTime dtEnd = new DateTime(2011,5,1,23,0,0) ; // 11pm 1 May 2011
TimeSpan tmElapsed = dtEnd - dtBegin ; // tmElapsed is a TimeSpan with a value of 60 minutes
To return the minutes, do something like:
int elapsedTimeInMinutes = (int) Math.Round( tmElapsed.TotalMinutes , 0 ,MidpointRounding.ToEven ) ;
var timeInMinutes = new DateTime(2011, 12, 25).Subtract(new DateTime(2010, 1, 1)).TotalMinutes;
Instead of creating the DateTime objects using the constructor I used, you can use DateTime.Parse, or better still DateTime.ParseExact to convert the strings to date times. (I know I am only using date parts here but you choose only to use time parts if you wish)
Convert DateTime objects to TimeSpan's, substract and call TimeSpan.TotalMinutes (or smth like that - dont' have VS at hand):
DateTime dt1, dt2;
// Assign some dates, then:
TimeSpan ts = dt2 - dt1;
double minutes = ts.TotalMinutes;