Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to extract date from timestamp

Consider the following timestamp

timestamp <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland")

> timestamp
[1] "2011-08-10 14:00:00 NZST"

What is the simplest way to get the day part 2011-08-10 from it, and making sure this day is a proper date and not a string?

Using lubridate::day(timestamp) obviously fails here.

like image 358
ℕʘʘḆḽḘ Avatar asked Oct 24 '16 15:10

ℕʘʘḆḽḘ


People also ask

How do I get just the date from a timestamp?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

How do I extract a date from a timestamp in Excel?

While working with Excel, we can extract only the date portion by using the INT or TRUNC function.

How do I extract a date from a timestamp in python?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.


3 Answers

This would probably be the simplest way:

date(timestamp)

It will return a date class and not a string.

like image 172
doron Avatar answered Nov 18 '22 15:11

doron


Use date instead of day

lubridate::date(timestamp)
like image 30
Praveen Kumar Avatar answered Nov 18 '22 15:11

Praveen Kumar


There is also data.tables as.IDate() function now:

timestamp <- "2011-08-10 14:00:00"
data.table::as.IDate(timestamp)
like image 37
andschar Avatar answered Nov 18 '22 16:11

andschar