Convert Numeric Value to DateTime [duplicate] - r

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"

Related

R: converting original excel dates to the format yyyy-mm-dd HH:MM:SS [duplicate]

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"

How to convert datetime from character to normal datetime in R [duplicate]

This question already has an answer here:
Milliseconds in POSIXct Class
(1 answer)
Closed 1 year ago.
I have a dataframe in r which consists of a column with the following DateTime format with character class.
dt<-2021-03-01 06:10:31.346
I need to convert it into the following format with datetime class.
2021-03-01 06:10:31
I have tried the following solution but it didn't work.
Df$Dt<- as.POSIXct(Df$Dt,format="%Y-%m-%dT%H:%M:%S")
The T is not needed as it was not present in the original string
as.POSIXct(dt, format="%Y-%m-%d %H:%M:%S")
#[1] "2021-03-01 06:10:31 CST"
If we want to take care of the microseconds
as.POSIXct(dt, format="%Y-%m-%d %I:%M:%OS")
Also, the format is not really needed here as the input is in the accepted format for as.POSIXct
as.POSIXct(dt)
#[1] "2021-03-01 06:10:31 CST"
data
dt <- '2021-03-01 06:10:31.346'

Date conversion and date formatting [duplicate]

This question already has answers here:
Convert 12 hour character time to 24 hour
(2 answers)
Closed 4 years ago.
How to convert 6/5/2017 12:00:00 AM -04:00 date and time to lubridate mdy_hms with 24 hours in R? I've tried
as.POSIXct("6/5/2017 12:00:00 AM -04:00",format = "%m/%d/%Y %hh:%M:%S", tz = "UTC")
but it return in 12 hours format and not 24 hours, I have data frame worth of 2 years data therefore I need to be able to return 2pm as 14 and 2am as 2.
I think your formatting string is wrong.
?strptime describes the formatting string. The string below is untested - if it doesn't work, check ?strptime and update it.
Your formatting string currently is correct for the day string, but for the time string it implies a 24hr number and ignores the AM/PM.
Edited: actual string should be:
as.POSIXct("6/5/2017 12:00:00 AM -0400",format = "%m/%d/%Y %I:%M:%S %p %z", tz = "UTC")
NB the timezone must NOT have a colon in it so that may need string manipulation to remove first.

Convert string to Date in R: month abbreviated "dd-mmm-YYYY" [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 4 years ago.
I have a vector of strings which I need to convert to dates. This only works for some of the dates which are in the format of "dd-mmm-YYYY".
Example: this works: strptime("21-Sep-2017", format = "%d-%b-%Y")
This does not work and returns NA: strptime("21-Dec-2017", format = "%d-%b-%Y")
What am I doing wrong or not seeing?
This is because your locale is probably one where December is not abbreviated as Dec. Without changing your session settings, you could simply do
lubridate::parse_date_time("21-Dec-2017", orders = "d-b-Y", locale = "us")
[1] "2017-12-21 UTC"

Number of seconds to date conversion [duplicate]

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"

Resources