YSK the date format YYYY-MM-DD is the international standard. It's a great format to use anywhere you put dates. It's especially useful when including dates in file names. This will allow all files to sort by date when sorting alphabetically.
CMV: MM/DD/YYYY Is almost always the best format for dates.
r - Converting year and month ("yyyy-mm" format) to a date? - Stack Overflow
sql - Convert date to YYYYMM format - Stack Overflow
Videos
Why YSK: YYYY-MM-DD is the International (ISO) Standard format for dates. This format makes the most sense for reasons of sorting and also eliminates any confusion due to different date formatting in different regions. This is especially useful when you have files in a folder that have dates in the name.
The world should get on the same system. I'm sure that the fact that the US (and maybe other countries) use MM/DD/YYYY and others use DD/MM/YYYY causes tons of inefficiencies and waste worldwide. So the question remains, what is the best way to standardize ourselves?
The US has a lot of things wrong, but in terms of date format, we're superior.
It is most efficient and logical for people to use MM/DD/YYYY because it most closely aligns with the way people think. The day is the most precise, important information, the year can in many cases be inferred or omitted, but the month is the most useful data point to receive/deliver FIRST.
When someone is going to be putting something into their calendar, digital or otherwise, what are they looking for first? The month is the primary piece of orienting information that the brain seeks when trying to locate a date. Even if the date in question is a future year, I'd bet most people advance forward through the months on a computer calendar rather than skip to January 2026.
MM/DD/YYYY most accurately reflects how we as humans receive and use time information, and as such should be the standard going forward.
The exception should be when naming computer files that should be organized in chronological order, in which case the logical naming convention should be YYYY/MM/DD.
Note that while MM/DD/YYYY is superior, MM/DD/YY is not. Any efficiency gained by the omission of 20XX is eclipsed by the potential confusion involved with not knowing which of the two optimal formattings are being expressed.
Since dates correspond to a numeric value and a starting date, you indeed need the day. If you really need your data to be in Date format, you can just fix the day to the first of each month manually by pasting it to the date:
month <- "2009-03"
as.Date(paste(month, "-01", sep=""))
Try this. (Here we use text=Lines to keep the example self contained but in reality we would replace it with the file name.)
Lines <- "2009-01 12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386"
library(zoo)
z <- read.zoo(text = Lines, FUN = as.yearmon)
plot(z)
The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of ?plot.zoo .
The zoo series, z, that is created above has a "yearmon" time index and looks like this:
> z
Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Aug 2009 Sep 2009 Oct 2009
12 310 2379 234 14 1 34 2386
"yearmon" can be used alone as well:
> as.yearmon("2000-03")
[1] "Mar 2000"
Note:
"yearmon"class objects sort in calendar order.This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of
zto"Date"class:time(z) <- as.Date(time(z)).