简短版本:有没有办法将两个NSDates
(可以由两个不同的字符串组成)组合成一个NSDate
对象?
详细信息:在我的应用中,有两个文本字段,用户分别在其中输入日期和时间(这些字段是使用自定义的日期选择器输入的)。当我想将信息保存在数据库中时,对象的属性需要一个日期,由日期和时间组成。
以下是我设置字符串的方法:
if ([mode isEqualToString:@"date"])
{
self.dateFormat = [[NSDateFormatter alloc] init];
[self.dateFormat setDateFormat:@"MM/dd/yy"];
self.dateTextField.text = [self.dateFormat stringFromDate:date];
}
else if ([mode isEqualToString:@"time"])
{
self.timeFormat = [[NSDateFormatter alloc] init];
[self.timeFormat setDateFormat:@"hh:mm a"];
self.timeTextField.text = [self.timeFormat stringFromDate:date];
}
其中date
是传递给方法的NSDate
。现在,当我尝试以其他方法保存信息时,我可以访问字符串和日期格式,因此我能够这样做
NSDate* tempDate = [self.dateFormat dateFromString: self.dateTextField.text];
NSDate* tempTime = [self.timeFormat dateFromString: self.timeTextField.text];
但这是我所能得到的...我不确定如何将日期组合成一个实体。我会以某种方式将日期格式化程序字符串组合在一起吗?
感谢您的帮助:)
试试这个。
NSString *strTemp = [NSString stringwithFormat:@"%@ %@",self.dateTextField.text,self.timeTextField.text];
NSDateFormatter *dateFotmatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFotmatter setDateFormat:@"MM/dd/yy hh:mm a"];
NSDate* tempDate = [self.dateFotmatter dateFromString:strTemp];
希望对您有所帮助。
享受编码。