Why isn't toLocaleDateString working in react-native (Android)?
React Native used JavaScriptCore engine in non-debug mode & it doesn't work well with dates, but work during debug because it use chrome V8 engine while debugging.
So it is better to use moment or XDate JavaScript library.
SDushan is right. It is better to use either Moment or XDate.
But I chose to write a small function instead of importing the whole library.
function getDateString(date) {
if (Platform.OS === 'ios')
return date.toLocaleDateString('en-US', {
weekday: 'short',
day: 'numeric',
month: 'long',
year: 'numeric',
});
else {
var
dayOfWeek = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"],
monthName = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"],
utc = date.getTime() + date.getTimezoneOffset() * 60000,
US_time = utc + (3600000 * -4),
US_date = new Date(US_time);
return dayOfWeek[US_date.getDay()-1] + ", " + monthName[US_date.getMonth()] +
" " + US_date.getDate() + ", " + US_date.getFullYear();
}
}