Parsing Date Time in different formats automatically in R - r

I have been working in R and parsing Date Time using as.POSIXct with specific format strings. But today I noticed that as.POSIXct converted "8/1/2017 11:00:28 PM" into "2017-08-01 11:00:28" with format="%m/%d/%Y %H:%M". The PM was not present in format and time were read wrongly since PM was not considered. Is this expected?
Is there a way in R to know if the parsing is done properly?
Also are there any R packages that can handle datetime in different formats & parse properly.

Related

WP All Import date issue

I am importing an excel file that has a date field with the following format: dd/mm/YYYY
It imports the data OK, however it does something strange with the dates. If the day is higher than 12, then it takes the current date.
I found this on the log:
WARNING: unrecognized date format ``, assigning current date
For example: the date 08/04/2020 is imported ok because 08 <= 12, however the date 23/02/2020 is not imported because 23 is not <= 12, and then it takes the current date.
Any ideas about what is happening?
This issue can get confusing with Excel, since typically, if the system is set for U.S. locales, Excel converts standard European date format in day-month-year to some version of month/day/year. In other words, tThe forward slash usually indicates American format, which is month first, as in April 9, 2022 or 04/09/22. You can force day/month/year, and apparently that was done for your file or system, but that's not an expected or standard format.
Without examining All Import code in detail, I'd say the behavior you describe implies it is presuming the standard usage. I'd therefore recommend that you try converting your date column before attempting imports, but how to do that efficiently would probably be more an Excel question than a WordPress question, unless you are able in your version of All Import to perform a custom conversion on the field.
A trick that sometimes works on this type of problem is copy-pasting Excel data into a Google Sheet, then downloading as CSV, without ever opening the file in your system before re-uploading. Google Sheets is pretty good about such conversions in general, but no guarantees: Your source file might defeat it in this case.
I had the same issue. Change the dates in your csv file to mm/dd/yyyy and when it imports it will automatically convert to dd/mm/yyyy if that is what your WP website is set up to use.
Only numbers lower than 12 will work the way you are importing because there are only 12 months in a year and it is reading you dd as mm.

Date is wrongly converted when imported into RStudio from Oracle

I'm working with an Oracle database and have a connection in RStudio using the ROracle package. For some reason some dates are converted when imported into R through either dplyr or dbGetQuery.
A date field that in the database reads 2018-01-01, turns into 2018-01-31 23:00:00 when imported. The same is the case with 2018-02-01 that is converted to 2018-02-28 23:00:00.
What is really weird is that if I export the data frame to an excel spread sheet using openxlsx the dates are again displayed correctly.
Anybody who knows what is going on, or could point me in the right direction? The column is formatted as POSIXct, and I´ve tried changing locale and timezone. I´ve also tried converting the date column with as.Date, but with no luck.
The problem had to do with how ROracle converts dates when importing. The dates for the winter months where imported as CET while the rest of the dates where imported as CEST.
Found the explanation here: https://www.oralytics.com/2015/05/r-roracle-and-oracle-date-formats_27.html

Write_csv output issues: yyyymmddThhmmssZ instead of yyyymmdd hhmmss

I have been messing around with the datetime formatting, as I have data that goes through daylight savings time. The csv files I read in skip an hour in the spring and then repeat one in the fall. This seems to mess up a loop I am using. So I am tying to see if telling R what timezone the data is in PST and PDT, will help.
I was playing with reading in the data:
read_csv("file.csv", locale = locale(tz = "America/Los_Angeles"))
I read in of the data inputs the proper timezone, but now I have issues using write_csv.
When I write my datafile it outputs ie. "2011-11-30T14:20:51Z" in the csv datetime column.
I tried to reinstall R/RSTUDIO, readr, and to read in new data without this formatting and write_csv; all with no luck! I am still learning so any tips would be appreciated, thank you!

What is the correct date format for writexl

What is the correct date format for the new writexl package? I tried it on lubridate dates, and the resulting Excel spreadsheet contains strings of the form yyyy-mm-dd (i.e. not Excel dates).
The purpose of the writexl package is to essentially create a mirror image of an R table in an excel file format. So having data in one of the usual R date/time formats like as.Date, as.POSIXct, etc. won't translate to a date format shifting from YYYY-mm-dd to d/m/y while being exported to excel. If you'd like it in a more standard excel date/time format in the excel file, it's best to convert it prior to exporting it with something like the strftime() function, like this:
require(writexl)
write_xlsx(
data.frame(date=strftime(c("2017-09-10","2017-09-11","2017-09-12"),"%d/%m/%y")),
"~/Downloads/mydata.xlsx")
Output (in xlsx file):
date
10/09/17
11/09/17
12/09/17
Edit:
If you'd like the data to be an Excel date format once it's in the new file, then adding as.POSIXct() to the above will ensure that.
It worked when I converted my dates to POSIXct dates using as.POSIXct.

Excel download date format changing in different time zone

How, i can keep date format same of excel at the time of download in different-2 time zone?
My downloaded file contains date column format is MM-dd-YYYY.
The issue is that, while i am downloading the excel file in US region it is not converting the dates into different formats, while i am downloading the same file in UK region, it is converting the dates into dd-MM-YYYY.
Date 12-01-2015 is converting to 01-12-2015.
I want to keep date format of excel file MM-dd-YYYY in all the regions.
I am generating the file using ASP.Net C#.
Thanks in advance.
This is because Excel is selecting the different date format based on the program's locale setting. I would imagine your options are:
a: Change the cell format to what you need in each location, e.g. in VBA the code would be something like Selection.NumberFormat = "m/d/yyyy;#"
b: Store your date data in text format. So literally a text string in mm-dd-yyyy format. You could still easily parse it later.

Resources