Home > Back-end >  How to Convert Date Time With Fractional Seconds and Additional Character
How to Convert Date Time With Fractional Seconds and Additional Character

Time:10-25

I am struggling to get date/time values converted. The issue is that the time component contains fractional seconds, and there are additional characters in the string that should be filtered out.

The format is of the current data/time variable is:

2018-12-30T18:03:23.400Z

The result I am looking for is:

2018-12-30 18:03:23.4

I have tried reproducing from these stack overflow questions: Parse string with additional characters in format to Date and Convert a large character string to dates, but the dates have a non numeric character

But I haven't been able to get them to work.

CodePudding user response:

The anytime package (which I wrote) aims to take the pain out of date (and datetime) parsing. It covers your format (with the one caveat about the incoming timezone which few parsers cover) as it does cover others:

> library(anytime)
> anytime("2018-12-30T18:03:23.400Z")
[1] "2018-12-30 18:03:23.4 CST"
> 

If you want just one digit shown, select that explicitly. I tell my R sessions to default to six:

> Sys.time()
[1] "2022-10-24 16:41:34.745706 CDT"
> options(digits.secs=1)
> Sys.time()
[1] "2022-10-24 16:41:49.9 CDT"
> 
  • Related