This question already has an answer here:
DateTime.fromMillisecondsSinceEpoch not returning an exact DateTime object
(1 answer)
Closed last month.
I am storing several rows in Postgres (Supabase) and want to use a timestampz field both as the date of creation as well as to "group" these rows.
When I use the now() of postgres the timestampz has microseconds, whereas the DateTime.now() of flutter has only milliseconds.
See image that shows the difference
What I am doing now is to write the first record (using the column default set to now()), read it to get the timestamp and then set this for the other rows.
Is there a way to generate DateTime.now() with microseconds in flutter?
You can use millisecondsSinceEpoch to get milliseconds from datetime.
final now = DateTime.now().millisecondsSinceEpoch;
print(now);
This will return you milliseconds since the "Unix epoch" 1970-01-01T00:00:00Z.
https://api.flutter.dev/flutter/dart-core/DateTime/millisecondsSinceEpoch.html
Related
I am using below condition to truncate date in postgres
to_date(to_char(trunc(appointment_date),'YYYYMMDD')||appointment_end_time,''YYYYMMDDHH24:MI:SS')AS tq
How I can use this in postgres ?
Strange data typing, sometimes requires strange, looking at least, queries. Try (see fiddle)
date_trunc('day',appointment_date)
+ substr(appoinment_end,12)::interval
As your to_char() call uses the format 'HH24:MI:SS' for the "time" column, you can cast that column directly to a time value, e.g. using the :: operator: appointment_end_time::time.
To build a new timestamp from the date part of the appointment_date and the time value, just add them:
appointment_date::date + appointment_end_time::time
So first the timestamp is converted to a date (that does not have a time), and then the time value is added to that, which yields a timestamp.
Note that to_date() returns a date so your code would remove the just added time part again. You would need to use to_timestamp() if you really want a timestamp as the result.
To answer the question's title "how to truncate date in Postgres?" (which in reality refers to a timestamp not a date): you can either cast it to a date (see above) or you can use date_trunc() (not trunc()) with a unit to which it should be truncated. However, date_trunc returns a timestamp not a date value, so you couldn't add a time to the result.
I exported data from an SQLite table to a CSV file. The data includes a timestamp with at least one-minute resolution: "2019-11-15 01:30:06". The data is actually stored as a Julian date, in this case 2458802.35424295. I imported the data into a double-precision field. I need to convert that number into a timestamp with time zone. I tried casting the double-precision number to text and then using to_timestamp(), but that appears to work only with integer days. I can get a timestamp, but it is always at midnight of the correct date. I tried using to_timestamp() passing in my number, but that returns an epoch (number of milliseconds since 1/1/1970).
I could try to take the fractional part of my Julian date value, calculate the number of milliseconds since midnight that represents, use the to_timestamp(text,text) method to get the date I need, and then add the epoch since midnight to that date. But that's awfully cumbersome. Isn't there a better way?
I'm using PostgreSQL 9.3.
NOTE: The simple answer to my problem, which occured to me just before I clicked the Post button, is to export the data in the form I want, using SQLite's datetime() function to convert the number to a date string during export. But I remain curious. I would have thought there would be a standard way to do this conversion.
Converting epoch time to SQL datetime format. I am trying to convert the 35 000 records received from another table (other db) with epoch timestemp to a new table with SQL datetime format. Also I will need to have this updated on a daily basis so one time conversion is good but I am also open to other suggestions.
I never worked with epoch and I am not sure how to go about it.
Also later on I want to use it in SSRS with correct datetime so I am not sure should I convert it before the transfer to new table or to do it in SSRS?
Any ideas?
Presuming that the epoch timestamp you have is in seconds:
DATEADD(SECOND, epoch_col, '19700101')
This will add the epoch seconds to the start of 'epoch time' (01-01-1970 00:00:00) and give you a DATETIME.
Example with output:
SELECT DATEADD(SECOND, 1571994774, '19700101')
2019-10-25 09:12:54.000
If you have an epoch timestamp in milliseconds just use this variation:
DATEADD(MILLISECOND, epoch_col, '19700101')
In terms of your other question about when to convert the value; My view is that it would be preferable to store the value in a DATETIME column at point of insertion rather than storing the epoch value and converting it upon use.
This is just an opinion though and not a recommendation.
I'm rather new to ASP.NET and SQL, so I'm having a tough time trying to figure out how to compare two time columns. I have a timestamped column and then a Now() column in an .mdb database. I need to have a gridview display records that are "Greater than or equal to 3 hours" from the timestamp. Any idea how I can accomplish this?
The Transact-SQL timestamp data type is a binary data type with no time-related values.
So to answer your question: Is there a way to get DateTime value from timestamp type column?
The answer is: No
You need another column of datetime2 type and use > operator to for comparison. You might want to set default value of getutcdate() to set it when each row is inserted.
UPDATE:
Since the column is of datetime type and not timestamp type (there is a type in SQL Server called timestamp, hence the confusion) you can just do
WHERE [TimeCalled] <= DATEADD(hour, -3, GETDATE())
Make sure your server is running in the same timezone as your code. It may be safer to store all dates in UTC. In that case use GETUTCDATE instead on GETDATE
Timestamps are generally used to track changes to records, and are updated every time the record is changed. If you want to store a specific value you should use a datetime field.
If you're using a DateTime Column and you want the result in TSQL try
DATEDIFF(Hour, 'Your DateTime Column here', 'pass Now() here' )
try to execute this example in TSQL:
select DATEDIFF(Hour, '2012-11-10 00:00:59.900', '2012-11-10 05:01:00.100')
I'm currently trying to do it that way:
// Creating date object
$date = new Zend_Date();
// Adding to it 4 weeks
$date->add('4', Zend_Date::WEEK); // it's expire day
// Getting date in integer(i guess it's unix timestamp yes?)
$date->get();
// Saving it to Mysql in field 'expire' with type Varchar
Then, when needed to get rows, that have date bigger(that haven't yet expired), than current I just add to SQL a simple statement WHERE expire >= $current_date.
Or there is better way to do it? Or how it happens usually?
I would recommend using the native MySQL DATETIME column in your table. This is how you'd retrieve the date for MySQL:
$date->get('yyyy-MM-dd HH:mm:ss');