6

I have to do a difference between dates and return the number of days. the format of the dates is as follows:

12/9/2011 12:00:00 AM

Does anyone know how to perform the difference without using lubridate?

Dario Federici
  • 1,228
  • 2
  • 18
  • 40
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Jul 06 '16 at 07:26

2 Answers2

13

We can use asPOSIXct to convert to DateTime

v1 <- as.POSIXct("12/9/2011 12:00:00 AM", format = "%d/%m/%Y %I:%M:%S %p")

If we need only Date

as.Date(v1)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • It' converting only a part of them: AEST" "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" [9948] "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" "2011-09-12 AEST" [9955] NA NA NA NA NA NA NA – Dario Federici Jul 06 '16 at 07:24
  • 2
    @DarioFederici In that case, the NA you showed may be of different format. – akrun Jul 06 '16 at 07:26
  • OK the solution is: v1 <- as.POSIXct(txns$Transaction_Date, format = "%m/%d/%Y %I:%M:%S %p") – Dario Federici Jul 06 '16 at 07:28
3

Convert datetime column in date format

df$DATETIME<-as.Date(df$DATETIME)
j__carlson
  • 1,346
  • 3
  • 12
  • 20
Rupesh Kumar
  • 157
  • 3