can someone tell me how to cast System.Timespan? to System.Timespan
I keep getting this error when tryin to get the date difference between the current date and a date from a linq query (see belwo)
System.TimeSpan ts = i.joinDt - DateTime.Now.Date;
System.TimeSpan ts = (i.joinDt - DateTime.Now.Date).Value;
To get a TimeSpan from a TimeSpan? you need to access the Value property of the nullable - no need to cast.
TimeSpan? tsn = i.joinDt - DateTime.Now.Date;
TimeSpan ts;
if(tsn.HasValue)
{
ts = tsn.Value;
}
Or:
if(i.joinDt.HasValue)
{
TimeSpan ts = i.joinDt.Value - DateTime.Now.Date;
}
can someone tell me how to cast System.Timespan? to System.Timespan
You need to specify a default value for the case where the TimeSpan? is null:
TimeSpan? nullableTs = ...
TimeSpan ts = nullableTs ?? TimeSpan.Zero;
Related
This question already has answers here:
Adding a TimeSpan to a given DateTime
(8 answers)
Closed 3 years ago.
A bit silly question, but I find myself not sure how to answer it.
Timespan ts = (DateTime1 - DateTime2).TotalMinutes
Suppose I know ts and DateTime2, how can I find DateTime1?
You can add a TimeSpan to a date
TimeSpan ts = DateTime1 - DateTime2;
DateTime1 = DateTime2 + ts;
Note that I removed the TotalMinutes, because it returns a double, not a TimeSpan.
If you want to work with minutes, you can write
double minutes = (DateTime1 - DateTime2).TotalMinutes;
DateTime1 = DateTime2 + TimeSpan.FromMinutes(minutes);
Note that the minutes contain the seconds and fractions of seconds as decimals. If you only need the full minutes, you can get them with:
int fullMinutes = (int)Math.Floor(minutes);
Your code does not compile.
Example:
DateTime DateTime1 = DateTime.Now;
DateTime DateTime2 = DateTime1.AddMinutes(-10);
var ts = (DateTime1 - DateTime2).TotalMinutes; // ts is a double
DateTime DateTime3 = DateTime1 + TimeSpan.FromMinutes(ts);
I have method which the datatype is TimeSpan. How can I convert it to a string?
This is my Model code:
private TimeSpan starttime;
public TimeSpan ShiftStartTime
{
get { return starttime; }
set
{
starttime = value;
OnPropertyChanged("ShiftStartTime");
}
}
This is my ViewModel code:
ShiftStartTime = ??reader[3].ToString()
The reader here represents my MySqlDataReader.
If your time format is "HH:mm:ss" then you can simply do:
ShiftStartTime = TimeSpan.Parse(reader[3].ToString());
If for example the value in your db is "20:30:21" then the TimeSpan object will store:
Hours: 20
Minutes: 30
Seconds: 21
You can use DateTime.ParseExact() method as below. You can pass format accordingly.
TimeSpan ts = DateTime.ParseExact(
reader[3].ToString(),
"HHmmss",
System.Globalization.CultureInfo.InvariantCulture
).TimeOfDay;
Let's say I have
int seconds = 43200;
(amount of seconds from the beginning of the current day, 00:00:00) and I want to get related DateTime representation ("12:00:00"). Is there any c# utility function?
You need the TimeSpan, then you can get the DateTime in this way:
TimeSpan timeOfDay = TimeSpan.FromSeconds( seconds );
DateTime dt = DateTime.Today.Add( timeOfDay );
It is not a DateTime representation, it looks like a TimeSpan representation to me instead.
For this, you can use TimeSpan.FromSeconds method like;
int seconds = 43200;
var ts = TimeSpan.FromSeconds(seconds);
If you really need to add this to generate current day midday, you can use DateTime.Today property and add this to that.
DateTime dt = DateTime.Today.Add(ts);
You can calculate it directly:
int seconds = 43200;
DateTime dateTime = DateTime.Today.AddSeconds(seconds);
I have an instance of DateTime that I get from my database, I want to subtract it from DateTime.Now and find out if 4 hours were passed. How do I do that?
Also when should i use DateTime.UTCNow or DateTimeOffset
You can use the subtraction operator to get a TimeSpan:
private static readonly TimeSpan MinimumTime = TimeSpan.FromHours(4);
...
if ((dateFromDatabase - DateTime.Now) > MinimumTime)
{
...
}
As for whether you need UTCNow or Now... it will depend on what happens to time zones when you fetch the data from the database. DateTime is not terribly clear on this front :(
If you can fetch the value as a DateTimeOffset to start with, then you can use DateTimeOffset.Now instead and it should be simpler to work out any time zone issues.
DateTime.Subtract
First Google hit..
Try this:
bool fourHoursPassed = date.AddHours(4) < DateTime.Now;
or this to actually perform a subtraction:
bool fourHoursPassed = (DateTime.Now - date).TotalHours > 4;
DateTime.Subtract
or
DateTime myDateTime = someValue;
TimeSpan ts = DateTime.Now -myDateTime;
if(ts.Hours>=4)
{
doSomething();
}
Hope it helps.
DateTime dt = new DateTime(2011, 07, 10);
DateTime dob = new DateTime(1987, 07, 10);
You can simply subtract as:
TimeSpan age = dt - dob;
I want to change a value from int or string format to datetime format. There is any function in SQL like the following?:
Function: Result
TimeAdd( nextrundate,"sec",45) 00:00:45
TimeAdd( nextrundate,"min",45) 00:45:00
TimeAdd( nextrundate,"hour",4) 04:00:00
But:
TimeAdd( nextrundate,"min",70) 01:10:00
TimeAdd( nextrundate,"min",190) 03:10:00
Is there a method that does this in C# also?
You mean:
TimeSpan.FromSeconds(double)
TimeSpan.FromMinutes(double)
see MSDN: http://msdn.microsoft.com/en-us/library/system.timespan_members(VS.80).aspx
System.TimeSpan s = new TimeSpan();
s.Add(new TimeSpan(days, hours, minutes, seconds, milliseconds))
In SQL you could use something like
convert(varchar(8),dateadd(second,45,nextrundate),114)
convert(varchar(8),dateadd(minute,45,nextrundate),114)
convert(varchar(8),dateadd(hour,4,nextrundate),114)
In C# you can use the DateTime and TimeSpan classes.
DateTime rundate = DateTime.Now();
DateTime nextRunDate= rundate .AddDays(1);
TimeSpan oneDay=(TimeSpan)(nextRunDate-rundate);
There are similar methods for minutes, seconds etc.
DateTime d = new DateTime();
d = d.AddMinutes(70);
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));
DateTime d = new DateTime();
d = d.AddMinutes(70);
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));