如何在字符串和 nsdate 中获得相同的日期?
how to get same date in string and nsdate?
我在日期格式化程序中遇到了问题。我添加我的代码如下:
NSDate *currentDate = [NSDate date];
NSLog(@"Current Date: %@",currentDate); //Current Date: 2016-10-14 08:07:15 +0000
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
NSLog(@"Current TimeZone: %@",tzName); //Current TimeZone: Asia/Kolkata
NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSString *SyTZName = [systemTimeZone name];
NSLog(@"System TimeZone: %@",SyTZName); //System TimeZone: Asia/Kolkata
NSString *strCurrentDate = [NSString stringWithFormat:@"%@",[NSDate date]];
NSLog(@"Current Date: %@",strCurrentDate);//Current Date: 2016-10-14 08:07:47 +0000
NSDateFormatter* localDateFrm = [[NSDateFormatter alloc] init] ;
[localDateFrm setTimeZone:[NSTimeZone timeZoneWithName:@"IST"]];
[localDateFrm setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSString* local_string = [localDateFrm stringFromDate:currentDate];
NSLog(@"local_string: %@",local_string);//local_string: 14-10-2016 01:37 PM
NSDateFormatter* local_dateFormatter = [[NSDateFormatter alloc] init] ;
[local_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"IST"]];
[local_dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSDate *date = [local_dateFormatter dateFromString:local_string];
NSLog(@"date : %@",date);//date : 2016-10-14 08:07:00 +0000
最后我需要 01:37 PM
但我得到 08:07:00 +0000
。当我将字符串转换为 NSDate 时,它没有给我预期的回复(01:37 PM)。所以时间不一样了。
所以请帮助我。
谢谢
NSDate
将始终 return utc
或 gmt
日期,如果您希望它在您当地的时区,则需要将其转换为带有 [= NSDateFormatter
.
的 14=] 方法
因此,当您想使用当地时区显示日期时,只需将其转换为 string
然后显示即可。当你想要它作为 NSDate
时,你可以使用 dateFromString
方法轻松地将字符串转换为日期,它会再次 return 日期在 utc or gmt
中。
通常你需要在 label
或任何 UI
中显示你的日期,所以你必须需要字符串,你不能在标签上显示 nsdate
。所以,这是处理 NSDate
的标准方法
更新:
您也可以将此日期用作 localnotification
的开火日期。执行下面的演示,
NSString *str = @"2016-10-14 08:07:00";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [df dateFromString:str];
NSLog(@"date : %@",[df dateFromString:str]);
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = date;
notification.alertTitle = @"title";
NSLog(@"notification : %@",notification);
并检查日志中的通知,它将根据您当地的时区显示开火时间!!
您可以设置的第二件事 timezone
也用于 notification.timeZone = ...;
等通知
我在日期格式化程序中遇到了问题。我添加我的代码如下:
NSDate *currentDate = [NSDate date];
NSLog(@"Current Date: %@",currentDate); //Current Date: 2016-10-14 08:07:15 +0000
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
NSLog(@"Current TimeZone: %@",tzName); //Current TimeZone: Asia/Kolkata
NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSString *SyTZName = [systemTimeZone name];
NSLog(@"System TimeZone: %@",SyTZName); //System TimeZone: Asia/Kolkata
NSString *strCurrentDate = [NSString stringWithFormat:@"%@",[NSDate date]];
NSLog(@"Current Date: %@",strCurrentDate);//Current Date: 2016-10-14 08:07:47 +0000
NSDateFormatter* localDateFrm = [[NSDateFormatter alloc] init] ;
[localDateFrm setTimeZone:[NSTimeZone timeZoneWithName:@"IST"]];
[localDateFrm setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSString* local_string = [localDateFrm stringFromDate:currentDate];
NSLog(@"local_string: %@",local_string);//local_string: 14-10-2016 01:37 PM
NSDateFormatter* local_dateFormatter = [[NSDateFormatter alloc] init] ;
[local_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"IST"]];
[local_dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSDate *date = [local_dateFormatter dateFromString:local_string];
NSLog(@"date : %@",date);//date : 2016-10-14 08:07:00 +0000
最后我需要 01:37 PM
但我得到 08:07:00 +0000
。当我将字符串转换为 NSDate 时,它没有给我预期的回复(01:37 PM)。所以时间不一样了。
所以请帮助我。
谢谢
NSDate
将始终 return utc
或 gmt
日期,如果您希望它在您当地的时区,则需要将其转换为带有 [= NSDateFormatter
.
因此,当您想使用当地时区显示日期时,只需将其转换为 string
然后显示即可。当你想要它作为 NSDate
时,你可以使用 dateFromString
方法轻松地将字符串转换为日期,它会再次 return 日期在 utc or gmt
中。
通常你需要在 label
或任何 UI
中显示你的日期,所以你必须需要字符串,你不能在标签上显示 nsdate
。所以,这是处理 NSDate
更新:
您也可以将此日期用作 localnotification
的开火日期。执行下面的演示,
NSString *str = @"2016-10-14 08:07:00";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [df dateFromString:str];
NSLog(@"date : %@",[df dateFromString:str]);
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = date;
notification.alertTitle = @"title";
NSLog(@"notification : %@",notification);
并检查日志中的通知,它将根据您当地的时区显示开火时间!!
您可以设置的第二件事 timezone
也用于 notification.timeZone = ...;