I'm working on a project where I'm given the Microsoft date/time format, which seems to be exactly like a standard ISO formatting.
When I try to format a date with Moment.js, it doesn't come out right. For example, the format I'm provided with has 'yy' for the 2-digit year and 'd' for a 1-digit day-of-month.
When I look at the Moment.js format documentation I realize the it doesn't even support it? That's a bit odd? Why would it not support a standard?
Or am I doing something wrong? Am I missing something?
I'd really hate to try to write an ISO to Moment.js format converter.
Has anyone had the same problems? If so, how have you resolved this?
Update 2017.10.16 17:32:
After getting a good question from Matt Johnson down in the comment, I read my post again and realized I've been working for so long in this "embedded" web project that I probably wasn't quite clear as to what is meant by "windows giving me a date format". So I wrote the following in response to his question:
@MattJohnson, what I mean is that the web project I'm working on for a client is an IE embedded (OLE) inside a Windows application (compiled binary, not web). The JavaScript portion of the web application "talks" to the desktop application in order to receive/send data. One of the data I receive is a "dateFormat" and "timeFormat" property. The values I receive, I'm told come directly from the Windows machine (based on user configuration of that OS). It happens that years/days are all lowercase, causing Moment.js to not being able to format dates properly. Thus the conversion I now have.
Examples:
Calling
moment().format();
Will produce
"2017-10-13T13:24:47-04:00"
Now, if I want to format it according to the Moment.js documentation, I'd do this:
moment().format('MM/DD/YYYY');
To get this:
"10/13/2017"
The problem is that Windows passes along this format:
"dd/MM/yyyy"
So when I call Moment with it, like:
moment().format('dd/MM/yyyy');
Giving me this:
"Ve/30/yyyy"
While I was expecting this:
13/10/2017
When I look at many other date formatting libraries, I see that they all support the 'yyyy', 'dd' and 'MM' structure. But not Moment.js.
Update 2017.10.13 16:41:
I did a comparison of Microsoft's date/time format to that of Moment.js and saw the differences.
* ---------------------------------------------------------------------------------------------
* Unit Microsoft Examples Moment.js Differnces?
* ---------------------------------------------------------------------------------------------
* day d, dd 1, 01 D, DD Yes, case
* day of week ddd, dddd Fri, Friday ddd, dddd None
* month M, MM, MMM, MMMM 8, 08, Oct., October M, MM, MMM, MMMM None
* year yy, yyyy 17, 2017 YY, YYYY Yes, case
*
* hour h, hh, H, HH 3, 03, 15, 15 h, hh, H, HH None
* minutes m, mm 9, 09 m, mm None
* seconds s, ss 5, 05 s, ss None
*/
Using that information, I quickly wrote a format conversion function:
function windowsFormatToMomentJSFormat(windowsFormat) {
var format = windowsFormat;
console.log("Converting date format...");
if (!windowsFormat) return format;
console.log(" > From : '" + windowsFormat + "'");
format = format.replace(/y/g, 'Y'); // Fix case for years
format = format.replace(/d{4}/g, '#'); // Protect 4-digit DOW sequence
format = format.replace(/d{3}/g, '&'); // Protect 3-digit DOW sequence
format = format.replace(/d/g, 'D'); // Fix case for days
format = format.replace(/#/g, 'dddd'); // Restore our sequence
format = format.replace(/&/g, 'ddd'); // Restore our sequence
console.log(" > To : '" + format + "'");
console.log(" > Applied : '" + moment().format(format) + "'");
return format;
}
It seems to work well, although I wish I was much better at advanced REGEX in order optimize the function and remove the protect/restore code.
So now, my Windows format strings seems to be processed correctly by Moment.js' format() function.
Yes, you will need a conversion function, because date/time formatting tokens are different across various libraries and platforms. There isn't one already developed, that I am aware of.
Note that your current function is missing several tokens. In particular the am/pm separator needs translation, and you should think about literals and escape sequences as well.