This question already has answers here:
Convert UNIX epoch to Date object
(2 answers)
Closed 5 years ago.
I have a data set where one of the columns is sales date. Don't know why, but R converts it to numeric why performing any operation. I would like to convert it back to POSIXct date format in R. To do the same, I am using below code, but getting an unexpected result
x= as.Date(1448208000, origin = "1970-01-01")
[1] "3967028-10-31"
x= as.POSIXct(x,"%Y-%m-%d")
I am not good with dates format in R and would appreciate any kind of help in this regard.
1448208000 is the number of seconds since the unix epoch, and is the numeric representation of a POSIX object. To convert it back to POSIXct you want
as.POSIXct(1448208000, origin = "1970-01-01")
You'll also probably want to ensure the timezone is correct too; see the difference between these two commands
as.POSIXct(1448208000, origin = "1970-01-01", tz = "UTC")
# [1] "2015-11-22 16:00:00 UTC"
as.POSIXct(1448208000, origin = "1970-01-01", tz = "Australia/Melbourne")
# [1] "2015-11-23 03:00:00 AEDT"
Related
This question already has answers here:
Converting excel DateTime serial number to R DateTime
(5 answers)
Closed 7 months ago.
I've read an excel files with dates in different formats into R. Some are correctly read in the format "yyyy-mm-dd HH:MM:SS" and those who have had another format in excel before are now numbers like:
44586.727083333302 (stands for 25.01.2022 17:27:00)
I tried to convert them:
as.Date(df$dates, format='%Y-%m-%d %H:%M:%S', origin = "1900-01-01 24:00:00")
But R gives me just yyyy-mm-dd and HH:MM:SS is missing.
I need the timestamp as well. Does anyone know how the code must be?
You'll have to use the as.POSIXct() function instead of the as.Date() function. as.Date() returns the day without the time. The formatting you did should be the same.
Update: request OP:
You should install parsedate package:
library(parsedate)
#your df:
date
1 2018-06-30 12:09:34
2 44586.727083333302
# with this code:
dat %>%
mutate(x = parse_date(date))
you get this
date x
1 2018-06-30 12:09:34 2018-06-30 12:09:34
2 44586.727083333302 2022-07-31 15:39:06
First answer:
We could use the convertDateTime function:
library(openxlsx)
string <- 44586.727083333302
convertToDateTime(string, origin = "1900-01-01")
#or for your data frame:
convertToDateTime(df$dates, origin = "1900-01-01")
[1] "2022-01-25 17:27:00 CET"
This question already has answers here:
Lubridate as_date and. as_datetime differences in behavior
(2 answers)
Closed 1 year ago.
I have the below numeric value. I need to convert it into datetime format.
1627074000000
To see more values like this (for e.g. 1627077600000), you can refer the link below -
https://coindatadesktop.com/coins/getRelatives.php?symbol=VNDC&limit=48&modo=pre
I googled a bit and found (here) it's in GMT format.
<startTimeGmtMs>1627243200000</startTimeGmtMs>
<endTimeGmtMs>1627248600000</endTimeGmtMs>
I tried this but returns invalid date -
as.POSIXct(1627243200000, origin = "1970-01-01", tz = "GMT")
This seems to be in miliseconds:
try:
as.POSIXct(1627243200000/1000, origin = "1970-01-01", tz = "GMT")
[1] "2021-07-25 20:00:00 GMT"
I have some MATLAB serial date number that I need to use in R but I havt to convert them to a normal date.
Matlab:
datestr(733038.6)
ans =
27-Dec-2006 14:24:00
you can see it gives the date and time.
Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(733038.6)
[1] "2006-12-27"
It gives only the date but I need also the time? Any idea
The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of time for the "POSIXct" class in R is, ‘1970-01-01 00:00.00 UTC’. You can read about how different systems handle dates here.
Before converting, you need to account for this difference in reference from one format to another. The POSIX manual contains such an example. Here's my output:
> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"
Where 719529 is ‘1970-01-01 00:00.00 UTC’ in Matlab's datenum and 86400 the number of seconds in an standard UTC day.
This question already has answers here:
Round a POSIX date (POSIXct) with base R functionality
(3 answers)
How to drop minutes in R?
(2 answers)
Closed 4 years ago.
I added a column to record the timestamp in R by using the following code
data$Date <- Sys.Date() 2018-08-10 18:06:21
This pastes a time-stamp with the current system date and time.
However, I want to set the time in the time-stamp to 00:00:00.
I tried using strptime and replacing the text. Thanks in advance.
There are multiple ways to do this , one way is to format your time part from Sys.time() to the required time.
data$Date <- format(Sys.time(), "%Y-%m-%d 00:00:00")
Sys.time returns current system date and time
Sys.time()
#[1] "2018-10-10 05:12:56 GMT"
format(Sys.time(), "%Y-%m-%d 00:00:00")
#[1] "2018-10-10 00:00:00"
Or if you want to use Sys.Date you can wrap it in as.POSIXct
data$Date <- as.POSIXct(Sys.Date())
I have a data frame with time in character format, I need to convert it into time format. I have tried using strptime and POSIXct but it adds the date also. I just need the time.
For e.g.: TRK_DEP_TIME <- c("22:00", "14:30"......) _____ character datatype
doing........ as.POSIXCT(TRK_DEP_TIME, format = %H:%M")
The result will be ("10/11/17,22:00", "10/11/17, 14:30".....)
I am looking for just the time, I don't need the date to be associated with it. Is there any way I can achieve this?
Use chron "times" class:
library(chron)
ch <- c("22:00", "14:30") # test input (character)
times(paste0(ch, ":00"))
## [1] 22:00:00 14:30:00