arshaw / xdate

A Modern JavaScript Date Library
http://arshaw.com/xdate/
GNU General Public License v2.0
681 stars 81 forks source link

corrupted locales in react native #42

Open tobiaseisenschenk opened 5 years ago

tobiaseisenschenk commented 5 years ago

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.

erbud commented 3 years ago

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

tobiaseisenschenk commented 3 years ago

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
    );
}