This question already has answers here:
Convert epoch to date in sqlplus / Oracle
(2 answers)
Closed 3 years ago.
I am running a query on an oracle db and am trying to get the start time for this particular event. The date is just stored as a number in the table and the only way I have been able to retrieve it is with TO_CHAR(TIMEFRAME_START). This gives me an output of 1574402400.
I know that this should correspond to 11/21/2019 11:00:00 PM MST based on a different app that pulls the same information but I have no idea how it does this conversion. I need to retrieve additional rows and need to be able to convert that output to a standard date format.
Does anyone have any ideas on how I can accurately format the original output?
Assuming that the number represents a unix timestamp, you can turn it to a date with the following expression:
to_date('1970-01-01', 'yyyy-mm-dd') + timeframe_start / 60 /60 / 24
Rationale:
unix epoch starts on January 1st, 1970
in Oracle, you can add a fractional number of days to a date
So basically we would convert the epoch timestamp to a number of days by dividing it by 60 (seconds per minutes), 60 (minutes per hour), and 24 (hours per day), and then add it to the date that represents epoch start.
You can then format the resulting date to the desired string format with to_char():
to_char(
to_date('1970-01-01', 'yyyy-mm-dd') + timeframe_start / 60 /60 / 24,
'mm/dd/yyyy hh12:mi:ss am'
)
You should do the following since that is a UNIX timestamp:
TO_CHAR(TO_DATE('19700101000000','YYYYMMDDHH24MISS') + numtodsinterval(TO_CHAR(TIMEFRAME_START), 'SECOND'), 'MM/DD/YYYY HH:MI:SS AM')
Related
This question already has answers here:
How to get the unix timestamp in C#
(17 answers)
Closed 8 months ago.
Can somebody help identify the format of this date and time (timestamp). THe webhook I am currently trying to have a link to has a security requirements where I need to get the timestamp. However, the format of the timestamp is new to me. See below the format:
The formatted timestamp converted to string should look like this:
1496734173
I have no idea how do I convert a date and time into something like this. I don't know what this code is or what time does it actually tells.
Click Here for the format
That looks like a pretty standard UNIX epoch timestamp. Assuming we're using the UTC (GMT) timezone, the date is Tuesday, June 6, 2017 7:29:33 AM.
UNIX time is the amount of seconds that have passed since Jan 1, 1970. The timestamp means 1496734173 seconds have passed since then, which is about 47 and a half years, i.e. June 6, 2017.
You can convert a DateTime object to a UNIX timestamp in the following way:
DateTime dateTime = DateTime.Now; // this would be your DateTime
DateTimeOffset offset = new DateTimeOffset(dateTime);
long epoch = offset.ToUnixTimeSeconds(); // our epoch is a 64 bit integer, i.e. long
Or, in one line:
long epoch = new DateTimeOffset(dateTime).ToUnixTimeSeconds();
I think that this represents the UNIX timestamp.
The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.
Here you can try and convert your timestamp here: check your time
This question already has answers here:
DateTime difference in days on the basis of Date only
(3 answers)
Closed 8 years ago.
I'm using ASP.NET, C# and SQL Server. In the database the date is: 2014-10-28. Just year, month and day, no time portion.
I have this code:
DateTime data1 = new DateTime();
DateTime data2 = DateTime.Now;
data1 = reader.GetDateTime(3);
double total= (data2 - data1).TotalDays;
Response.Write(total.ToString());
The only problem of this is the output. The output of this is "4,81351624131366". Because probably this is the difference of the hours. Its possible to set the data2 to give me just the Y,M and D?
Or convert the total into days?
The property TotalDays returns the fractional portion of days too. Since the date you're comparing was 4 days ago at midnight, you're getting 4 full days and an additional 4/5th of a day (since it's not exactly midnight when you're executing this).
Either what Jon said (use DateTime.Today instead of DateTime.Now to get the date portion with a time of midnight), or use the Days property, which will just return a rounded 4 and drops the fractional portion.
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)
I have a very simple issue with datetime and am seeking some help.
I have a log that I would like to get all data information from. There are three columns of datetime formats (2 in UNIX timestamp while the other isn't).
The one with different timestamp format offers a value of, for example, 22194885 which I don't know which datetime type it belongs to.
Looks like minutes since January 1, 1970. this is Python code, but works the same as C localtime():
>>> import time
>>> time.localtime(22194885*60)
time.struct_time(tm_year=2012, tm_mon=3, tm_mday=13, tm_hour=19, tm_min=45, tm_sec=0, tm_wday=1, tm_yday=73, tm_isdst=1)
Works out to 3/13/2012 7:45pm.
Looks like it could be minutes since the Epoch, rather than milliseconds
22194885 minutes / 60 = 369914.75 hours
369914.75 hours / 24 = 15413.1 days
15413.1 days / 365 = 42.2 years
1970 + 42.2 = about today
For help converting Epoch time to .Net time, see
How to convert a Unix timestamp to DateTime and vice versa?
Remember that question deals with milliseconds, so you'll have to adjust the answer slightly.
Based on the calculations in Eric J's answer (which has been deleted), this could well be the number of minutes since the epoch. Bah! He slipped in a ninja edit.
Julian seconds (since the start of the year) is also a strong possibility.
I am trying to build a nice, small database to run on a mobile application (Windows Mobile 5, if you are curious).
In the SQLite Documentation, the Date and Time Datatype is defined as follows:
1.2 Date and Time Datatype
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic
Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.
So, saving my DateTime value as either a REAL (float) or INTEGER is the same size.
What about the TEXT format? There are 23 characters above in the text YYYY-MM-DD HH:MM:SS.SSS. Is that 8-bytes per character? If so, that is a HUGE waste of space to store in Text format (which is what I am currently doing).
What about the REAL format? Would I define a base date of November 24, 4714 B.C.? (I am not even sure if Visual Studio 2008 will let me do that. I've never tried.) Then get the TimeSpan between base date and date I want, extract the number of days, and store that?
// is this how to declare this date?
private static readonly DateTime nov24_4714bc = new DateTime(-4714, 11, 24);
public static double GetRealDate(DateTime dateTime) {
// FYI: subtracting dates in .NET returns a time span object
return (dateTime - nov24_4714bc).TotalDays;
}
What about the INTEGER format? Would I define a base date of 1970-01-01 00:00:00 UTC (please tell me how to do that!), then get the TimeSpan between base date and my input date, extract the number of seconds, and store that?
// is this a UTC date?
private static readonly DateTime utc1970_01_01 = new DateTime(1970, 1, 1);
public static double GetIntDate(DateTime dateTime) {
// FYI: subtracting dates in .NET returns a time span object
return (dateTime - nov24_4714bc).TotalSeconds;
}
Any help with this? I am a little confused on a few points.
Use the TEXT format if "human-readability" is important.
Use one of the numeric formats if saving space is important.
If you don't need millisecond precision, you can save space in the TEXT format by only including the part you do need. There are 3 shorter formats accepted by SQLite date/time functions:
YYYY-MM-DD HH:MM:SS (19 characters)
YYYY-MM-DD HH:MM (16 characters)
YYYY-MM-DD (10 characters)
(NEVER use MM/DD/YYYY; it's not supported, and it doesn't sort correctly.)
Would I define a base date of November 24, 4714 B.C.? (I am not even
sure if Visual Studio 2008 will let me do that. I've never tried.)
You can't: System.DateTime only supports the years 1 to 9999. You need to pick a different base date, and then do (dateTime - baseDate).TotalDays + baseDateJD, where baseDateJD is the Julian date of the base date. Some reasonable choices are:
0001-01-01 = JD 1721425.5
1970-01-01 = JD 2440587.5
2000-01-01 = JD 2451544.5