[英]Convert static time to NSDate returns older date
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"hh:mm a"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setLocale:[NSLocale systemLocale]];
NSLog(@" >> %@ >>> %@",date,[df dateFromString:@"12:18 AM"])
And log is 和日志是
>> 2014-04-10 07:00:48 +0000 >>> 2000-01-01 06:52:00 +0000
I want that the second date has also the same date as the first. 我希望第二个日期也与第一个日期相同。
like 喜欢
2014-04-10 07:00:48 +0000 >>> 2014-04-10 06:52:00 +0000
If you want to convert the time string "12:18 AM" to a date for the current day then you can proceed as follows: 如果要将时间字符串“ 12:18 AM”转换为当前日期,则可以按以下步骤进行:
NSString *time = @"12:18 AM";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
// Get year-month-day for today:
[fmt setDateFormat:@"yyyy-MM-dd "];
NSString *todayString = [fmt stringFromDate:[NSDate date]];
// Append the given time:
NSString *todaysTime = [todayString stringByAppendingString:time];
// Convert date+time string back to NSDate:
[fmt setDateFormat:@"yyyy-MM-dd h:mm a"];
NSDate *date = [fmt dateFromString:todaysTime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
By using this formatter, you can get NSDate
通过使用此格式化程序,您可以获得
NSDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.