Extract Time from Timestamp

extract time from timestamp in js

Javascript has an in-built function for this. You can use any of the below based on your need.

var timestamp = 1480687432 * 1000;
console.log(new Date(timestamp).toTimeString());
console.log(new Date(timestamp).toLocaleTimeString());

Teradata SQL: Extract time from Timestamp

If it's already a Timestamp data type, you can use cast like this:

CAST(fieldname AS TIME)

Or if it's still a string, you would have to do this I think:

CAST(CAST(fieldname AS TIMESTAMP FORMAT 'dd-mm-yyyyBhh:mi:SS.s(1)') AS TIME)

(I forget the proper syntax for the timezone identifier)

Extracting time from timestamp variable

First of all, you are not using mysqli properly. Please learn about prepared statements.

To get the data from the database you can use the following code:

$stmt = $mysqli->prepare('SELECT Timestamp FROM recentdata Where TagID=?');
$stmt->bind_param('s', $tagID);
$stmt->execute();
$result = $stmt->get_result();

If you are expecting only a single result, there is no need for a loop at all. Just get a single row without a loop.

$recentdata = $result->fetch_assoc();
echo "Old Timestamp: {$recentdata['Timestamp']}<br>";

To get a time from your timestamp, you need to convert it into a PHP DateTime object. Then you can use the format() method to get the time part only.

$dt = new DateTime($recentdata['Timestamp']);
echo "Time ". $dt->format('H:i:s'); // H = hours, i = minutes, s = seconds

You can do the same with the data you get from the user. Make sure that the timestamp follows a known format, or use date_create_from_format() and specify your own format.

$RTC_dt = new DateTime($RTCtimestamp);
echo "Time ". $RTC_dt->format('H:i:s'); // H = hours, i = minutes, s = seconds

R Extract time from timestamp then save as a new column

Maybe this can help:

# some fake data
df <- data.frame(a = 1, timestamp = '2018-11-11 12:13:14')

# convert as data
library(lubridate)
df$timestamp <- ymd_hms(as.character(df$timestamp, tz = "UTC"))

# add the hour:minutes column
library(dplyr)
df %>% mutate(hour_ = format(df$timestamp,'%H:%M'))
a timestamp hour_
1 1 2018-11-11 12:13:14 12:13

Or in only one dplyr chain:

df %>%
mutate(hour_ = format(ymd_hms(as.character(df$timestamp, tz = "UTC")),'%H:%M'))

How to extract the time from a timestamp without timezone column?

You can convert to a time and then use comparisons. For aggregation:

COUNT(*) FILTER (WHERE date_created_utc::time >= '08:00:00' and date_created_utc::time < '11:00:00') as cnt_1

Extracting Time from Timestamp in SQL

In Redshift you can simply cast the value to a time:

the_timestamp_column::time

alternatively you can use the standard cast() operator:

cast(the_timestamp_column as time)


Related Topics



Leave a reply



Submit