I am saving images with unique names. To get unique name I used DateTime.UtcNow.Ticks. Now I want to delete all images older than a day. How can I get the ticks older than a day?
I have mapped time with ticks.
1:52:14.312 PM - 633614215343125000
1:52:14.359 PM - 633614215343593750
1:52:14.421 PM - 633614215344218750
1:52:14.468 PM - 633614215344687500
1:52:14.515 PM - 633614215998593750
What is the best way to get ticks older than a day? OR how to get the ticks of 24 hours ago?
DateTime has a constructor which takes Ticks as parameter. Extract ticks from file name, parse them to long and create a DateTime type object. Later you can select records older than a day.
DateTime dt = new DateTime(633614215998593750);
To select dates older than a day use:
if(dt <= DateTime.Now.AddDays(-1))
TimeSpan.TicksPerDay will give you the ticks per day, so just subtract that from your current ticks to get the ticks 24 hours ago e.g:
var ticks24HoursAgo = DateTime.UtcNow.Ticks - TimeSpan.TicksPerDay;
This should do :
DateTime dt = new DateTime(ticks);
if (DateTime.Now - dt > TimeSpan.FromDays(1))
{
//do something
}
Also - you can work with UTC instead of local
Every DateTime object has a Ticks property, so just get 24 hours ago (add -1 days to DateTime.Now should be sufficient), and then delete all of the values that are less than the ticks of your calculated time.
Related
Let's say that DateTime.Now is 13:26. How do I get 12:00 from this? I need to find the latest full hour and then go back further one hour. Or go back one hour and then find the latest full hour, whichever way... How do I achieve this in C#?
I tried now.AddHours(-1); to remove the first hour, but I don't know how to go back to 00 minutes. Is there a good and clean way of doing this?
DateTime.Today.AddHours(DateTime.Now.Hour - 1);
This takes the current Date (with Time set to zero) and only adds the desired amount of hours back on.
No need to deal with minutes, seconds, ticks etc.
You could construct a new DateTime in order to truncate it, then take off an hour
var dt = DateTime.Now;
dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, dt.Kind).AddHours(-1);
This correctly deals with the hour being 0
Timezones and summer-time changes in particular confuse me. In the UK we have GMT/BST:
In the UK the clocks go forward 1 hour at 1am on the last Sunday in
March, and back 1 hour at 2am on the last Sunday in October. The
period when the clocks are 1 hour ahead is called British Summer Time
(BST).
Given a local time say 00:00 I want to be able to calculate how long until it is 03:00 in local time. Normally this is trivially 3 hours but on March 26th (last Sunday in March) from 00:00 - 03:00 is actually two hours. And similarly when the clocks go back in October from 00:00 - 03:00 is four hours.
Do the .Net DateTime class and its methods do this trivially for me or do I need to be careful?
In my case specifically I'm working from strings so I'm after a method doing:
TimeSpan DifferenceBetweenLocalTimes(string startDateTime,string endDateTime)
I can see things like TimeZoneInfo.IsDaylightSavingTime but how to use this to do as I wish is not obvious. My application works treating each calendar day's local midnight as a strict boundary i.e. not every day is 24 hours long, once a year I get a 23 hour day and a 25 hour day.
You can use TimeZoneInfo class to get the offset from your local date time to UTC (including daylight tricks). For example
var timeZone =TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
var date1 = DateTime.Parse("2017-03-26 00:00:00");
var date2 = DateTime.Parse("2017-03-26 03:00:00");
var dto1 = new DateTimeOffset(date1, timeZone.GetUtcOffset(date1));
var dto2 = new DateTimeOffset(date2, timeZone.GetUtcOffset(date2));
var diff1 = (dto2 - dto1).TotalHours;
Console.WriteLine(diff1); // this is 2 hours
The GetUtcOffset method returns difference between time in that time zone and UTC
While tchrikch's answer is perfectly reasonable (and should be accepted, IMHO), it's worth adding a Noda Time based solution.
// Parse input as LocalDateTime values (since they represent a local date and time)
var pattern = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt1 = pattern.Parse("2017-03-26 00:00:00").Value;
LocalDateTime ldt2 = pattern.Parse("2017-03-26 03:00:00").Value;
// Apply a specific time zone, now making them ZonedDateTime values
// Using "lenient" conversions allows for default handling of ambiguous/invalid values
DateTimeZone tz = DateTimeZoneProviders.Tzdb["Europe/London"];
ZonedDateTime zdt1 = ldt1.InZoneLeniently(tz);
ZonedDateTime zdt2 = ldt2.InZoneLeniently(tz);
// Now simply determine the elapsed duration between these
Duration result = zdt2 - zdt1;
Note that the subtraction between ZonedDateTime values was added in NodaTime 2.0. If you're on an older version, you'll need to do this instead:
Duration result = zdt2.ToInstant() - zdt1.ToInstant();
This is currentTime(EST) "2015-09-20 04:25:49.090". I need the below calculation.
Remaining time of "2015-09-20 04:25:49.090" this day (Ex : remaining time of this day is : 19 hours 35 minutes)
againg i have subtract from 24 hours - remainingTime (With minutes)
Could you please suggest me the solution for this?
Regards,
Arun
Sure - you do exactly as you've described, using TimeSpan. You can get the time of day from a DateTime using the TimeOfDay property, and then just subtract that from 24 hours:
// You could use TimeSpan.FromDays(1) as well
var remaining = TimeSpan.FromHours(24) - dateTime.TimeOfDay;
Now, one thing to be wary of is that this doesn't necessarily give you the amount of time left in the day - because "local" days can be different lengths depending on time zone changes (e.g. for daylight saving time). If you need to take that into account (e.g. that on November 1st at 00:30 in New York, there's 24 1/2 hours left in the day...). That's a more complicated question - especially if you also need to take account of time zones where the day doesn't always start at 00:00.
As for the second part of getting "24 hours - remaining time" - that's just "the time of day", as you've got 24 hours - (24 hours - x) which is just x.
Is there a way to convert a DateTimeOffset into minutes since midnight?
I want the offset part to be reflected in the answer it gives.
dateTimeOffset.UtcDateTime.TimeOfDay.TotalMinutes
returns the number of minutes since midnight for the original date/time from which the offset was subtracted
so if date/time = Jan 2 2013 and offset =8 hours, this will return 16 hours (since Jan 1 midnight)
How can I get the duration in terms of minutes by subtracting a previous time stamp stored in a DB from the present time in C#?
The previous time stamp format is 13:00.
I want to calculate how many minutes have passed. How to do it?
Plus the fact that the previous datetime Stamp is stored in a DB as e.g.13:00 (hh:mm). So I do not understand how I am able to substract 13:00 from the current DateTime.Now.ToShortString().
You can substract two DateTime objects and get a TimeSpan object. Then use TimeSpan.TotalMinutes.
DateTime dateTimeFromDatabase = DateTime.Parse("13:00");
double minutes = (DateTime.Now - dateTimeFromDatabase).TotalMinutes;