1

I have a data frame contain one date column it is in POSIXCT format as shown below

"2019-02-18 00:00:31 IST"

I want date in below format

 "2019-02-18" 

but when i applied

 x = as.Date( DATE ,"%Y%m%d") 

it is giving below warning

Warning message:
In as.POSIXlt.POSIXct(x, tz = tz) : unknown timezone '%Y%m%d'

And i am getting below wrong( one day previous date) output

"2019-02-17"

Plz let me know whats going wrong.

learner
  • 43
  • 1
  • 6

2 Answers2

1

You could do this also with strftime():

strftime(d, format="%Y-%m-%d")
[1] "2019-02-18"

With format= you can basically choose what you want to extract. It works even with just "%Y" in order to extract the year component.

Data:

d <- as.POSIXlt("2019-02-18 00:00:31 IST")
RLave
  • 8,144
  • 3
  • 21
  • 37
0

One option would be to just cast the POSIXct datetime to a character, and then take the first 10 characters:

date <- as.POSIXct("2019-02-18 00:00:31", format="%Y-%m-%d %H:%M:%OS", tz="Asia/Kolkata")
date
date.str <- as.character(date)
substr(date.str, 1, 10)

[1] "2019-02-18 00:00:31 IST"
[1] "2019-02-18"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360