Is there a way to convert a DateTimeOffset into minutes since midnight? - c#

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)

Related

How to remove time in date time?

How to remove a time in date time ? on column date its only display format
I store the value on repository combobox dropdown, and it store the value including the time. How do I remove the time?
I know there's so many question about this. But the solution was by converting it into a date.tostring("dd MMM yyyy"). Is there a solution beside convert it into string? I want the value was date time not a conversion of string.
The code I am using still giving me a time.
DateTime date = Convert.ToDateTime(gridView1.GetDataRow(i)["date"]);
You just forgot to specify the date at the end of the conversion
DateTime date = Convert.ToDateTime(gridView1.GetDataRow(i)["date"]).Date;
DateTime as the name implify, stores date and time.
You cannot remove time part from date because time is an integral part of date.
To understand this you will have to understand how the date and time are stored. Internally, the date and time is stored as a rational number (in fractions). In computer system 24 hours are considered as numeric 1, so when your value is increased by 1 that means your date is increased by 1 day. If the value is increased by 0.5 that means your date is increased by 12 hours (half day).
So, when you have value 42613.00 that means 31st August at midnight (just when the day started) and if you have value 42613.25 that means 6 AM of 31 Aug 2016 and 42613.50 means 12 noon of 31 Aug 2016 (and 42613.39236 means 9:25:00 AM of 31 Aug 2016)
The smallest fraction of time that need to be stored is 1 millisecond. That means the values of DateTime field should have a precision of more than 0.0000000115740740740741. But this is an irrational value (in binary) and hence cannot be stored as such (the nearest match is 1.00000000000000000000000000110001101101011101010000111010111111..., ... means there are more), so I can say that milliseconds are to their nearest approximation values.
.
That said,
if you wish to take only Date part, you can create your own class or struct to store date part of the DateTime and then override operators for date arithematic and provide implicit conversions to convert them to DateTime if any code that expect DateTime field.

Convert difference in milliseconds to .MaxValue back to Date/Time

For a rowkey on Azure TableStorage entities following prefix is used:
DateTime.MaxValue.Subtract(DateTime.UtcNow).TotalMilliseconds
As far as I know should this timestamp act as a kind of "sorter" so that newer entities are on top of an list. So, this shown code line creates (as I can imagine) the amount of milliseconds of the current date/time till the DateTime.MaxValue.
Is there a simple and safe way, to convert this amount of milliseconds "back" to the date/time when the timestamp was created? I´m not so familiar with date/time conversions...
The DateTime.MaxValue is:
equivalent to 23:59:59.9999999 UTC, December 31, 9999 in the
Gregorian calendar, exactly one 100-nanosecond tick before 00:00:00
UTC, January 1, 10000.
Thus, considering roughly 10,000 years, you have:
10,000 x 365 x 24 x 60 x 60 x 1000 = 315,360,000,000,000 //Note 15-digit
And the double precision is at least 15-digit. In other words, as long as you use the first 15 digit of your TotalMilliseconds as the timestamp, then it should be fine.
I recommend to cast it to long whose integer precision is:
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 //note, more than 15-digit
And then use ToString("D15") as unique timestamp:
long val = (long)DateTime.MaxValue.Subtract(DateTime.UtcNow).TotalMilliseconds;
string timestamp = val.ToString("D15");
And to convert back, you could cast it back to double and use AddMilliseconds with negative sign from max.
double db = Convert.ToDouble(timestamp);
DateTime dt = DateTime.MaxValue;
dt.AddMilliseconds(-db); //this will give you the datetime back with milliseconds precision
Then you will get precision up to your milliseconds.

get DateTime.UtcNow.Ticks older than a day

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.

How to get the remaining time from 24 hours in C#

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.

How to convert time from .net epoch to time in Unix epoch with Ruby?

I have an input timestamp from C# (.NET epoch: 00:00:00 (midnight), January 1, 0001) and I want to output it in Ruby world (Unix epoch: 00:00:00 UTC on 1 January 1970).
The input timestamp is given in UTC, and derived from .NET's DateTime(Int64), which is "a date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar."
The input timestamp: 634891434586852680
Output should be 2012-NOV-21 a bit after 5pm PST.
input = 634891434586852680
UNIX_EPOCH_IN_100NS_INTERVALS = 621355968000000000
Time.at((input-UNIX_EPOCH_IN_100NS_INTERVALS)*1e-7).utc.getlocal
=> 2012-11-21 17:10:58 -0800
In Ruby, Time is stored internally as the number of seconds with fraction since the Epoch, January 1, 1970 00:00 UTC. Therefore we must convert from 100-nanosecond intervals to seconds.
The conversion factor of 1e-7 is 1e2/1e9 which is 100/1000000000 which can be explained as:
X intervals * 100ns/interval * 1s/1000000000ns
The intervals cancel themselves out, as do the nanoseconds, and we are left with seconds; and 100/1000000000 seconds is 1e2/1e9 seconds which is 1e-7 seconds.

Categories

Resources