I have 2 DateTime's:
DateTime beginDate = 2000/01/01 14:00
DateTime endDate = 2000/01/01 14:30
I calculate a timespan between these 2 hours:
TimeSpan span = endDate.Subtract(beginDate);
var myValue = beginDate.AddMinutes( span.Minutes ).TimeOfDay.Ticks
//Trying to get this equal to the endDate by using the beginDate + myValue
//I have to use myvalue, because this value comes from the DB and this piece
//of code sits in another class than the above code
DateTime otherDate = beginDate.Date.Add( new TimeSpan( myValue ) )
The Problem is I keep on getting 0:30 back and I should get back 14:30.
I understand why, its because beginDate.Date gives you 2000/01/01 00:00, but I cant use beginDate.TimeOfDay.Add because it is a readonly field.
How do I achieve that it only add the myValue to the time of the given date??
You should use TimeSpan.TotalMinutes.
span.Minutes only gives you the minutes part. So of 1:30 it won't be 90. It'll be 30. Use TotalMinutes to get the 90.
Also beginDate.Date should be changed to beginDate because by using Date you are removing the time.
DateTime otherDate = beginDate + span;
Related
As in the title, I get the current date in seconds and I need to get the start of the day also in seconds, example function
private long? getStartOfDay(long? dateVal){
return ...
}
date with 0 number of seconds will return midnight Jan 1 1970
You mean like this?
private static long? getStartOfDay(long? dateVal) {
return dateVal - dateVal % 86400L;
}
Just truncating down to nearest multiple of 24 * 60 * 60.
Will not work with negative values, so either include a check that will throw an ArgumentOutOfRangeException, or change to ulong.
You are working with Unix date. To get DateTime from seconds:
double seconds = ...
// .Date to get rid of TimeOfDay (Hour, Minute, Second) component
DateTime startDate = DateTime.UnixEpoch.AddSeconds(seconds).Date;
Or if you want to get local time Date
DateTime startDate = DateTime.UnixEpoch.AddSeconds(seconds).ToLocalTime().Date;
To get seconds from DateTime:
DateTime myDate = ...
var seconds = (myDate.ToUniversalTime() - DateTime.UnixEpoch).TotalSeconds;
I have time format like 9:00AM - 10:00AM.
string startTime = "9:00AM";
string endTime = "10:00AM";
I want to display it like 9-10 AM.
How can I achieve it?
Parse your times
var sdt = DateTime.ParseExact(startTime, "h:mmtt", CultureInfo.InvariantCulture);
var edt = ...
Compare the meridian designators or check if the start is before 12 and the end is after:
if(sdt.Hour < 12 && edt.Hour >= 12)
If the times are in the same day half, use format ${sdt:h}-{edt:htt}"
If the times are in different day halves use format ${sdt:htt}-{edt:htt}"
If you have datetime, use datetime.ToString("HH");
If you don't, you can also write like that (remove AM before doing it)
Convert.ToDateTime(starttime).ToString("HH");
There are also easier ways to do it if you have fixed numbers;
starttime.Replace(":00","")
string starttime = "9:00AM";
string Endtime = "10:00AM";
starttime = starttime.Replace(":00AM","");
Endtime = Endtime.Replace(":00","");
var result = starttime + "-" + Endtime;
But i think you must change the Format of starttime and endtime to a TimeSpan or DateTime
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 am trying to make a string with start dates and end dates. That can loop over X number of days.
int nrOfDaysToLoopBy = 3;
List<string> Dates = new List<string>();
string startDate = "2014-01-01"; //this date is given to me by an api I am just hard codeing for testing
string endDate = "2014-01-30";
DateTime StartDate = DateTime.ParseExact(startDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
DateTime EndDate = DateTime.ParseExact(endDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
DateTime myDate = StartDate; // setting the initial date
for (DateTime date = StartDate.AddDays(nrOfDaysToLoopBy); date.Date <= EndDate.Date; date = date.AddDays(nrOfDaysToLoopBy))
{
Dates.Add("start-date=" + myDate.ToString("yyyy-MM-dd") + "&end-date=" + date.ToString("yyyy-MM-dd"));
myDate = date.AddDays(1);
}
// Dealing with any left over days.
if (myDate != EndDate && EndDate > myDate)
{
Dates.Add("start-date=" + myDate.ToString("yyyy-MM-dd") + "&end-date=" + EndDate.ToString("yyyy-MM-dd"));
}
My results:
start-date=2014-01-01&end-date=2014-01-04
start-date=2014-01-05&end-date=2014-01-07
The problem here is that there is 4 days between 2014-01-01 and 2014-01-04 not 3.
Results I am trying to get:
start-date=2014-01-01&end-date=2014-01-03
start-date=2014-01-04&end-date=2014-01-06
Update:
Can we agree that the days between 2014-01-01&end-date=2014-01-04 are
2014-01-01, 2014-01-02, 2014-01-03, and 2014-01-04 That's 4
I have been playing with this for a few hours now and nothing I have done has fixed the problem.
The problem here is that there is 4 days between 2014-01-01 and 2014-01-04 not 3.
One could argue that there's 3 days between 2014-01-01 and 2014-01-04: Between 2014-01-01 and 2014-01-04, three 24h periods pass:
2014-01-01 -- 2014-01-02
2014-01-02 -- 2014-01-03
2014-01-03 -- 2014-01-04
You see, the problem is how you define between. If you want to include the start and the end date, you need to adjust the number of dates you add.
Edit:
This fixed the problem. nrOfDaysToLoopBy-1
for (DateTime date = StartDate.AddDays(nrOfDaysToLoopBy-1); date.Date <= EndDate.Date; date = date.AddDays(nrOfDaysToLoopBy))
{
}
Simply change the statement:
DateTime date = StartDate.AddDays(nrOfDaysToLoopBy);
in your for loop initialization to
DateTime date = StartDate.AddDays(nrOfDaysToLoopBy-1);
The issue is that when you add days, the number of days "between" as you're calling it is actually the number of days you add + 1. So monday->monday (zero days added) is 1 day, monday->tuesday (one day added) is 2 days, etc. That's why you need the -1 modification for the start date.
The reason you don't need this alteration on every other iteration on the loop is because of the myDate = date.AddDays(1) line. This reduces the difference between myDate and date by one, bringing it back to the correct difference
If I have a date and time in a DateTime object, can I remove say 10 minutes, or 24 hours etc from the date and time using a format string?
So if I have 1/1/1990 12:30:00pm and I wanted to remove 1 hour from it, can I use a format string?
edit
i need to store diary entries and the user can select a reminder type. so 1 hour before hand. so then i'd like to store the format string in a db that i can get and apply to a datetime to get the reminder date time
Something like this might do what you need. Not sure what you mean by subtracting an hour using a format string though.
DateTime dt = new DateTime(1990, 1, 1, 12, 30, 0);
string s = dt.Subtract(new TimeSpan(1, 0, 0)).ToString("MM/dd/yyyy hh:mm:ss tt")
Best way is to convert to a DateTime object and then back to the formatted string (if that's what you need):
var time = DateTime.Parse("1/1/1990 12:30:00pm");
time = time.AddMinutes(-10);
string timeString = time.ToString("MM/dd/yyyy hh:mm:ss tt");
You can use this in your query if you have..
DATE_SUB(date,INTERVAL expr unit)
Example:
YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 1 HOUR)
YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 12 MINUTE)
YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 45 SECONDS)