我有一个数据报警器,并以字符串格式将值保存到.plist文件中,其中键为UserDate。
来自datepicker的值以2013-02-13 11:17:34 +0000格式返回。
为了将其保存到.plist文件中,我需要将数据采集器转换为字符串2013-02-14T19:17:34Z。
在其他视图中,我希望从plist检索UserDate并与用户当前日期进行比较。我想要不同的日子和时间。
我将2012-02-12 19:17:34 +0800转换为destinationDate,并将其与NSDate new进行比较。我设法计算出两个日期之间的差额。但结果是不正确的。
如何获得iPhone和Datepicker的日期之间的两个日期的正确差异结果?我需要使用特定的时区吗?
Ex外信息:我住在GMT+8。我把模拟器设定为2012年2月13日,但结果仍然显示倒计时还有18小时零4分钟。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[datelabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day],
'd', [components hour], 'h', [components minute], 'm', [components second], 's']];NSLog

编辑
Plist文件。

保存方法
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// set the variables to the values in the text fields
self.userTitle = myTitle.text;
self.userMessage = myMessage.text;
NSLog(@"METHOD SAVE");
NSLog(@"Datepicker value = %@", myDate.date );
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString *mdate = [dateFormatter stringFromDate:myDate.date];
// [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLog(@"Converted from date picker, save into plist = %@", mdate );
self.saveDate = mdate;
//self.userDate = myDate.date;
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: userTitle, userMessage, saveDate, nil] forKeys:[NSArray arrayWithObjects: @"Title", @"Message", @"UserDate", nil]]; NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your settings have been saved." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alert show];
}
else
{
NSLog(@"Error in saveData: %@", error);
//[error release];
}viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES];
[super viewWillAppear:animated];
// Data.plist code
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
NSLog(@"Plist Title: %@", [temp objectForKey:@"Title"]);
NSLog(@"Plist Message: %@", [temp objectForKey:@"Message"]);
NSLog(@"Plist date: %@", [temp objectForKey:@"UserDate"]);
userTitle.text = [NSString localizedStringWithFormat:@"%@", [temp objectForKey:@"Title"]];
userMessage.text = [NSString localizedStringWithFormat:@"%@", [temp objectForKey:@"Message"]];
//http://stackoverflow.com/questions/1070354/how-do-i-get-the-current-date-in-cocoa
NSDate* now = [[NSDate alloc] initWithTimeIntervalSinceNow:3600];
userCoundown.text = [NSString stringWithFormat:@"%@",now];
NSString *uDate = [[NSString alloc] init];
uDate = [temp objectForKey:@"UserDate"];
NSLog(@"Plist date in string - %@", uDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *ddate = [dateFormatter dateFromString:uDate];
[dateFormatter setDateFormat:@"yyyy-MM-dd H:mm:ss Z"];
NSLog(@"Format date from Plist - %@", [dateFormatter stringFromDate:ddate]);
//NSLog(@"GMT format from Plist - %@", timeStamp);
//http://www.epochconverter.com/#
destinationDate = ddate;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}更新标签
-(void)updateLabel {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[datelabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day],
'd', [components hour], 'h', [components minute], 'm', [components second], 's']];
}发布于 2012-02-12 15:13:40
NSDate在格林尼治时间内保持时间。如果您有时区,则必须指定它们。
这个问题还不清楚,所以这不是一个完整的答案,但是应该在plist中为NSDate提供一些帮助。
下面是一个用NSDate创建、编写和读取plist的示例
NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
userTitle, @"Title",
userMessage, @"Message",
saveDate, @"UserDate",
nil];
[plistDict writeToFile:filePath atomically:YES];
NSDictionary *plistDictRead = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSDate *dateRead = [plistDictRead objectForKey:@"UserDate"];不需要将NSDate转换为字符串,也不需要使用NSPropertyListSerialization。
发布于 2012-08-09 18:10:41
记住创建NSDate,然后用有效时区和日历输出它!
查看我在另一篇文章中回答的类似问题:UIDatePicker return wrong NSDate
https://stackoverflow.com/questions/9249615
复制相似问题