i am programming in c# and i have question:
how can i get the integer value between a date.
for example : 12/6/2010 and 12/18/2010
how can i get at 1st i=6 and in the second i=18
DateTime dt = DateTime.Parse("12/6/2010");
int i = dt.Day;
See: DateTime reference
It's not clear what you are asking, but you can access the day from a DateTime as follows:
DateTime dt=DateTime.Now;
int day=dt.Day;
If you want a time difference in days:
DateTime dt1=new DateTime(2010,12,6);
DateTime dt2=new DateTime(2010,12,18);
int dayDelta=(dt2-dt1).Days;
If you have a DateTime variable (birth, by example). you can use birth.Month to obtain the month, birth.Day to obtain the day, etc.
You can use "Days" method of DateTime class.
Related
How do I subtract two DateTime values from another DateTime value and save the result to a "DateTime" and than write the subtract time in label?
time_spend = (time_now - time_first) //example
lbl1.Text = time_spend.ToString("hh:mm:ss");
You can use someDateTime.Subtract(otherDateTime), this returns a TimeSpan which has a TotalDays property.
You can also Subtract() a TimeSpan and it will return a DateTime
I must have built up such that I have a datetime which gets added antale day as it should go forward. and then I have time as it should set off in relation to 04/10/16 to 10/09/16
I do not care about the time which is in datetime. It should not I use for anything. What I need out of this is exactly how many days there are from that time.
Datetime dateString = "4/10/2016 8:30:52" //I pretend that it comes from the database, it was more in terms of see what come there.
DateTime dt = DateTime.Now.AddDays(5);
What I need out of this is that it tell me how many days there are in between the two datetime as I entered.
You can substract DateTime objects to obtain a TimeSpan:
Datetime dateString = DateTime.Parse("4/10/2016 8:30:52");
DateTime dt = DateTime.Now;
TimeSpan duration = dt-dateString;
From the TimeSpan object, you can get how many (full) days with :
int totalCompleteDays = (int)duration.TotalDays;
Or if you want a rounded results :
int roundedTotalDays = (int)Math.Round(duration.TotalDays);
DateTime objects support basic operators and will return TimeSpan objects.
DateTime DateTimeB = DateTime.Now.AddDays(5);
DateTime DateTimeA = DateTime.Now;
TimeSpan difference = DateTimeA - DateTimeB;
...
you can then use the TotalDays property of the timeSpan.
...
Console.out.WriteLine(difference.TotalDays);
I'm trying to get today's date
DateTime todayDateTime = new DateTime();
and I'm getting this:
{1/1/0001 12:00:00 AM}.
Why is this happening?
Use this
DateTime date = DateTime.Now;
Using new DateTime() creates a DateTime with a time of "0".
If you want todays date you need to use DateTime.Today if you want a DateTime object with a date of today and a time of 12:00:00 AM or DateTime.Now if you want a DateTime with the day and time of the moment you called DateTime.Now.
According to MSDN, the constructor for DateTime which takes in a long initializes by using the specified number of ticks since January 1st, 0001, so saying new DateTime(0) yields this time, not the current time.
Instead, use the static field DateTime.Now to get a DateTime representing the current system time.
In your question you are just initializing the Variable todayDateTime but you have never assigned (set it). This is why it is date ("null")/ beginning of our time calculations.
To actually get todays Date, you can use the following:
DateTime today = DateTime.Today;
first of all you need to assigned a value in the datetime.
just use something like this :
DateTime today = DateTime.Today;
I have method which expects two datetime parameters
public void SomeReport(DateTime TimeFrom, DateTime TimeTo)
{
// ommited
TimeFrom.ToString("ddMMyy"), TimeTo.ToString("ddMMyy")));
// ommited
}
When I'm sending this params
DateTime TimeTo = DateTime.Now;
DateTime TimeFrom = new DateTime().AddHours(-1);
This error occured:
System.ArgumentOutOfRangeException : The added or subtracted value results in an un-representable DateTime.
What can be the problem?
new DateTime() is 01/01/0001 00:00:00 which is also DateTime.MinValue.
You are subtracting one hour from that.
Guessing you are trying to subtract an hour from the TimeTo value:
var TimeFrom = TimeTo.AddHours(-1);
new DateTime() returns the minimum representable DateTime; adding -1 hours to this results in a DateTime that can't be represented.
You probably want DateTime TimeFrom = TimeTo.AddHours(-1);
try:
DateTime TimeTo = DateTime.Now;
DateTime TimeFrom = TimeTo.AddHours(-1);
creating a DateTime with new DateTime() gives you a DateTime with DateTime.MinValue... from this you actually can't subtract anything... otherwise you get the exception you got... see MSDN
Look you date or time data .There not enough digits for date or time Example date must be 8 digit 20140604 and time 6 digit like this 180203.For this reason you are getiing error.
i get this error too and find time 18000 and change this to 180000 problem solved.
In your case TimeFrom holds the datetime from which -1 can not be added. You can either invoke
DateTime TimeFrom = TimeTo .AddHours(-1);
or
DateTime TimeFrom = new DateTime().now.AddHours(-1);
Both of them yield the same result.
In my error, I used the time as 24:00 instead of 00:00
Hi I have a local variable of the type of System.DateTime. How can I get just the time? Thanks
DateTime.Now.TimeOfDay returns a TimeSpan representing the time of the day.
If this is for display purposes (As it normally is with these questions), you can simply use one of the following:
myDateTime.ToShortTimeString();
myDateTime.ToString("hh:mm:ss"); //for 12 hour clock
myDateTime.ToString("HH:mm:ss"); //for 24 hour clock
DateTime time = DateTime.Now.TimeOfDay
DateTime dt = DateTime.Now;
string time_now =dt.TimeOfDay.ToString();