NSDateFormatter and yyyy-MM-dd

寵の児 提交于 2019-11-27 09:27:05

You are facing this problem because your date formatter is not correct.Suppose your newInvoice.date variable store "11:02:23" the your dateFormatter should be @"yy:MM:dd" and if your newInvoice.date variable store"2/22/11" then your dateFormatter should be @"MM/dd/yy"

NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];

[dateFormat1 setDateFormat:@"MM/dd/yy"];
[dateFormat2 setDateFormat:@"yyyy-MM-dd"];

dateTemp = [dateFormat1 dateFromString:newInvoice.date];
newInvoice.date = [dateFormat2 stringFromDate:dateTemp];

both format should be according to your requirement to get the correct result

Sana

This should work fine. I have set Date style. You can change NSDateFormatterShortStyle to something else. Also check your newInvoice.date format.

NSDate *dateTemp = [[NSDate alloc] init];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateStyle: NSDateFormatterShortStyle];

dateTemp = [dateFormat dateFromString: newInvoice.date];

[dateFormat setDateFormat:@"yyyy-MM-dd"];

newInvoice.date = [dateFormat stringFromDate:dateTemp];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!