Skip to content

why is as.date function not working for me? (dd/mm/yyyy h:mm)

2 messages · arun

#
HI,

The question is not clear.? If it is to get the hours,
?strptime(dates, format="%d/%m/%Y %H:%M")$hour
# [1]? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23? 0
#or
?as.numeric(format(as.POSIXct(dates, format="%d/%m/%Y %H:%M"),"%H"))
# [1]? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23? 0
#or
as.numeric(gsub(".*\\s+(\\d+)\\:.*","\\1",dates))
?#[1]? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23? 0
A.K. 


Why am I know getting hours after I convert the date? 

?dates <- c('31/12/2013 0:00', ?'31/12/2013 1:00', 
?'31/12/2013 2:00', ?'31/12/2013 3:00', ?'31/12/2013 4:00', ?'31/12/2013
 5:00', ?'31/12/2013 6:00', ?'31/12/2013 7:00', ?'31/12/2013 8:00', 
?'31/12/2013 9:00', '31/12/2013 10:00', '31/12/2013 11:00', ?'31/12/2013
 12:00', '31/12/2013 13:00', '31/12/2013 14:00', '31/12/2013 15:00', 
?'31/12/2013 16:00', '31/12/2013 17:00', '31/12/2013 18:00', '31/12/2013
 19:00', '31/12/2013 20:00', '31/12/2013 21:00', '31/12/2013 22:00', 
'31/12/2013 23:00', ?'01/01/2014 0:00') 

as.Date(dates, format="%d/%m/%Y %H:%M") 
?[1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
?[6] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
[11] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
[16] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
[21] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2014-01-01"
#
Hi,
You could use:
?strptime(dates, format="%d/%m/%Y %H:%M")[1:2]
#[1] "2013-12-31 00:00:00" "2013-12-31 01:00:00"
?as.POSIXlt(dates, format="%d/%m/%Y %H:%M")[1:2]
#[1] "2013-12-31 00:00:00" "2013-12-31 01:00:00"
A.K.


Thanks. as.POSIXct works for the most part. The only problem is part of 
data I'm working has it's own time zone. Is there a way to not have a 
time zone displayed? My times do not change with Daylight saving.
On Thursday, February 6, 2014 1:43 PM, arun <smartpink111 at yahoo.com> wrote:
HI,

The question is not clear.? If it is to get the hours,
?strptime(dates, format="%d/%m/%Y %H:%M")$hour
# [1]? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23? 0
#or
?as.numeric(format(as.POSIXct(dates, format="%d/%m/%Y %H:%M"),"%H"))
# [1]? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23? 0
#or
as.numeric(gsub(".*\\s+(\\d+)\\:.*","\\1",dates))
?#[1]? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23? 0
A.K. 


Why am I know getting hours after I convert the date? 

?dates <- c('31/12/2013 0:00', ?'31/12/2013 1:00', 
?'31/12/2013 2:00', ?'31/12/2013 3:00', ?'31/12/2013 4:00', ?'31/12/2013
5:00', ?'31/12/2013 6:00', ?'31/12/2013 7:00', ?'31/12/2013 8:00', 
?'31/12/2013 9:00', '31/12/2013 10:00', '31/12/2013 11:00', ?'31/12/2013
12:00', '31/12/2013 13:00', '31/12/2013 14:00', '31/12/2013 15:00', 
?'31/12/2013 16:00', '31/12/2013 17:00', '31/12/2013 18:00', '31/12/2013
19:00', '31/12/2013 20:00', '31/12/2013 21:00', '31/12/2013 22:00', 
'31/12/2013 23:00', ?'01/01/2014 0:00') 

as.Date(dates, format="%d/%m/%Y %H:%M") 
?[1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
?[6] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
[11] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
[16] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" 
[21] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2014-01-01"