我正在应用程序中构建聊天功能,并且正在使用 FaridSafi/react-native-gifted-chat。当我在 chrome 上调试时,消息日期很好。如果我不远程调试所有消息日期变为“无效日期”。我在真实设备和模拟器上有相同的结果
我使用此函数将我从 API 获得的日期格式化为库所采用的格式:
formatOneMessage(message) {
const receiver = this.props.navigation.getParam("receiver");
const receiverName = receiver.Name;
const receiverLastName = receiver.lastName;
const formatedDate = Util.formatDate(message.creation_date)
const FormatedMessage = {
_id: message.id,
text: message.content,
createdAt: new Date(formatedDate),
user: {
_id: message.sender_id,
name: receiverName + " " + receiverLastName,
avatar: "https://placeimg.com/140/140/any"
}
};
return FormatedMessage;
}
formatDate(date){
let dateAndTimeArray = date.split(" ");
let dateArray = dateAndTimeArray[0].split("-");
let newDate = dateArray[1] + "-" + dateArray[0] + "-" + dateArray[2];
let newDateAndTime = newDate + " " + dateAndTimeArray[1]
return newDateAndTime;
}
谢谢!