arshaw / xdate

A Modern JavaScript Date Library

Home Page:http://arshaw.com/xdate/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

corrupted locales in react native

tobiaseisenschenk opened this issue · comments

I am not sure if it is in the scope of this library, but I wanted to mention it here since I had the issue today.

We used XDate in a react-native app. Any formatting via .toString('M/d/yy h(:mm)TT'); as shown in the docs would cause the app to crash (not in the simulator, only on the device). The reason was the locales object missing the properties amDesignator and pmDesignator.

Feel free to close the issue, since XDate works fine inside browsers (https://jsfiddle.net/sfd05Lwn/1/). I fixed our issue by simply calculating AM/PM manually.

Hi @tobiaseisenschenk, can you better detail how you solved it?

Hi @tobiaseisenschenk, can you better detail how you solved it?

Well my dirty hack as I said is to not let xdate do the formatting for me. I parsed the time in 12h format like this:

function parse12h(xDate) {
	let hours = xDate.getHours();
	let minutes = xDate.getMinutes();
	let ampm = hours >= 12 ? 'PM' : 'AM';
	hours = hours % 12;
	hours = hours ? hours : 12; // the hour '0' should be '12'
	return (
		hours + ':' +
		(minutes < 10 ? '0' + minutes : minutes) +
		' ' + ampm
	);
}