0

What format is the date below and how can I convert it?

theData <- fromJSON(theUrl)
theData["sampleDate"]
$sampleDate
[1] 1.439165e+12

Looking at the data in a browser, sampleDate has the value 1439164800000. Using jsonlite (and kind of new to alot of this stuff)

Thanks

Selman
  • 1
  • 1
  • 3

1 Answers1

1

It is very likely that your data are milliseconds expressed in Unix time, that is, milliseconds (that's why I divide them by 1000) since 1970-01-01. Try this:

as.POSIXct(1.439165e+12/1000, origin = "1970-01-01", tz = "UTC")
[1] "2015-08-10 00:03:20 UTC"

Note that you have to use your right time-zone argument, here I've just used one to show that argument.

SabDeM
  • 7,050
  • 2
  • 25
  • 38
  • Yep! Is there any way to generalize it? Like: `as.POSIXct(theData["sampleDate"]/1000, origin = "1970-01-01", tz = "UTC")` – Selman Aug 20 '15 at 20:28
  • Yes that code should work. as.POSIXct can operate on a vector. – Rohit Das Aug 20 '15 at 21:37
  • As said by RohitDas your code should work because of vectorisations, that is that every element of data you pass to the function will divided by 1000 @Selman – SabDeM Aug 21 '15 at 02:19
  • @Selman unless we are dealing with lists. If the case you should provide a sample of your data with dput – SabDeM Aug 21 '15 at 02:20
  • Yes, it is a list for some reason. so I get an errror: `as.POSIXct(theData["sampleDate"]/1000, origin = "1970-01-01", tz = "UTC") Error in theData["sampleDate"]/1000 : non-numeric argument to binary operator` – Selman Aug 21 '15 at 09:07
  • Aha, just had to use unlist(). It says you are not supposed to write thanks, so I will just say it out loud. – Selman Aug 21 '15 at 09:12
  • @Selman does it work? it could be better if you provide a piece, with `dput` of your data, to we can help you better by seeing what kind of data you are dealing with. – SabDeM Aug 21 '15 at 11:36
  • It works if I `x <-unlist(theData["sampleDate"]) ` and use x, so I'll settle for that. (But didnt succeed in modifying the `theData["sampleData"]` to work with `as.POSIXct`) – Selman Aug 22 '15 at 08:23
  • @Selman most likely because that's a list. that's the reason why I asked you to provide a `dput` of your data and update the question with more detail. Users like me want to help but who asks a question has to provide the more detail possible in order to help users to help them. – SabDeM Aug 22 '15 at 13:05
  • great idea to divide the epoch values by 1000, I had a hard time figuring this out. Thanks . – Chamberlain Mbah Jan 22 '19 at 18:50