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.
Related
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"
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"
This question already has an answer here:
Dealing with timestamps in R
(1 answer)
Closed 6 years ago.
The timestamps I have as character class are in this format: 1/28/15 16:34 . How do I covert it to an R time stamp format and then also extract the hour of the day, day of the week and year separately as well?
You can use strptime function in this way:
my_time = strptime("1/28/15 16:34", "%m/%d/%y %H:%M")
Note in particular the %m and the %y to say, respectively, that months will be written with 1 character from Jan to Sept and year will be written with 2 character.
For example, if you need to convert "01/28/2015" you need %M and %Y:
my_time = strptime('01/28/2015 16:34', '%M/%d/%Y %H:%M')
To extract the day of week and the hour:
library(lubridate)
week_day = wday(my_time) # or wday(my_time, label=T) if you want the weekday label (Wed in this case)
day_hour = hour(my_time)
This question already has answers here:
How do I clear an NA flag for a posix value?
(3 answers)
Closed 6 years ago.
I'm hoping someone can shed light on why a few date/time values are being evaluated by is.na as TRUE when they in fact contain a valid date time value?
dateString = "03/09/14 02:00:00 AM"
dateValue <- strptime(dateString, format='%m/%d/%y %I:%M:%S %p', tz="")
is.na(dateValue)
but one hour earlier or later, and the vast majority of other dates and times, is.na correctly returns FALSE.
Other dateStrings I've experienced this 'error' with include
dateString = "03/08/15 02:30:30 AM"
dateString = "03/13/16 02:25:30 AM"
I updated my code to specify the GMT timezone as the data is collected in GMT without a change to or from daylight savings time.
dateValue <- strptime(dateString, format='%m/%d/%y %I:%M:%S %p', tz="GMT")
This ensures properly formatted date time values are not evaluated to TRUE with is.na()
This question already has answers here:
How to convert time stamp string "2014-07-20T05:11:49.988Z" into POSIXt in R?
(2 answers)
Closed 6 years ago.
I have the following data frame,the data set is already imported from a database table and created_at column has character type:
sale_id created_at
1 2016-05-28T05:53:31.042Z
2 2016-05-30T12:50:58.184Z
3 2016-05-23T10:22:18.858Z
4 2016-05-27T09:20:15.158Z
5 2016-05-21T08:30:17.337Z
6 2016-05-28T07:41:14.361Z
How can i extract only hour and minute from created_at column , preferably using base r libraries? i need to paste hour and minute together later and put it as a new column.
We can use the convenient functions in lubridate to convert the character column to DateTime and extract the hour and minute with format
library(lubridate)
v1 <- ymd_hms("2016-05-28T05:53:31.042Z")
format(v1, "%H:%M")
#[1] "05:53"
Or using only base R
format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"), "%H:%M")
#[1] "05:53"
Other options include with gsub
gsub(".*T|:\\d+\\..*", "", "2016-05-28T05:53:31.042z")
#[1] "05:53"
Using only base R libraries:
format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"), "%H:%M")
05:31
It appears that's UTC format. For more details on parsing that format see this.
Let me show it using Sys.Date() for an example as well:
format(as.POSIXlt(Sys.time(), "America/New_York"), "%H:%M")
08:15
Using the infinitely better lubridate library:
require(lubridate)
minute(ymd_hms("2016-05-28T05:53:31.042Z"))
53
second(ymd_hms("2016-05-28T05:53:31.042Z"))
31.042