我有一系列的每周日期,例如
1-5-2010至7-5-2010
我想要两个日期之间的唯一记录如何比较日期?
如何从当前日期获得当前周日期??
提前thnx ..//alert handler
#import <Foundation/Foundation.h>
@interface AlertHandler : NSObject {
}
+(void)showAlertForProcess;
+(void)hideAlert;
@end
#import "AlertHandler.h"
@implementation AlertHandler
UIAlertView *av;
UIActivityIndicatorView *actInd;
+(void)showAlertForProcess{
if(av!=nil && [av retainCount]>0){ [av release]; av=nil; }
if(actInd!=nil && [actInd retainCount]>0){ [actInd removeFromSuperview];[actInd release]; actInd=nil; }
av=[[UIAlertView alloc] initWithTitle:@"Loading" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
actInd=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[actInd setFrame:CGRectMake(120, 50, 37, 37)];
[actInd startAnimating];
[av addSubview:actInd];
[av show];
}
+(void)hideAlert{
[av dismissWithClickedButtonIndex:0 animated:YES];
if(av!=nil && [av retainCount]>0){ [av release]; av=nil; }
if(actInd!=nil && [actInd retainCount]>0){ [actInd removeFromSuperview];[actInd release]; actInd=nil; }
}
@end
///Sample of webservice..
-(void)callWebServiceForSignUp{
[AlertHandler showAlertForProcess];
if([gender.text isEqualToString: @"Male"]){
gen=[NSString stringWithFormat:@"1"];
}
else{
gen=[NSString stringWithFormat:@"0"];
}
NSString *soapMsg = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<AddUser xmlns=\"http://tempuri.org/\">\n"
"<UserName>%@</UserName>\n"
"<UserEmail>%@</UserEmail>\n"
"<FirstName>%@</FirstName>\n"
"<LastName>%@</LastName>\n"
"<Gender>%@</Gender>\n"
"</AddUser>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",username.text,email.text,firstname.text,lastname.text,gen];
NSURL *myURL=[NSURL URLWithString:[WebService getLoginURL]];
NSMutableURLRequest *connectionReq=[NSMutableURLRequest requestWithURL:myURL];
[connectionReq addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[connectionReq addValue:@ "http://tempuri.org/AddUser" forHTTPHeaderField:@"SOAPAction"];
[connectionReq setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[connectionReq addValue:[NSString stringWithFormat:@"%i",[soapMsg length]] forHTTPHeaderField:@"Content-Length"];
[connectionReq setHTTPMethod:@"POST"];
NSURLConnection *myConnection=[[NSURLConnection alloc] initWithRequest:connectionReq delegate:self];
if(myConnection){
myWebData=[[NSMutableData alloc] initWithLength:0];
}else{
UIAlertView *ConnectionNullAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
ConnectionNullAlert.message=@" can't able to connect to Server!";
ConnectionNullAlert.title=@"Message";
[ConnectionNullAlert addButtonWithTitle:@"OK"];
[ConnectionNullAlert show];
[ConnectionNullAlert release];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *ConnectionFailAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
ConnectionFailAlert.message=@" can't able to connect to Server!";
ConnectionFailAlert.title=@"Message";
[ConnectionFailAlert addButtonWithTitle:@"OK"];
[ConnectionFailAlert show];
[ConnectionFailAlert release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[myWebData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[myWebData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *str=[[NSString alloc] initWithBytes:[myWebData bytes] length:[myWebData length] encoding:NSStringEncodingConversionAllowLossy];
NSLog(@"%@",str);
[str release];
if(myXMLParser!=nil && [myXMLParser retainCount]>0){ myXMLParser.delegate=nil; [myXMLParser release]; myXMLParser=nil; }
myXMLParser=[[NSXMLParser alloc] initWithData:myWebData];
myXMLParser.delegate=self;
[myXMLParser parse];
[myWebData release];
}
#pragma mark XMLParser methods
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"AddUserResult"]){
tempDic=[[NSMutableDictionary alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(tmpStr!=nil && [tmpStr retainCount]>0){ [tmpStr release]; tmpStr=nil; }
tmpStr=[[NSString alloc] initWithString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"AddUserResult"] ||
[elementName isEqualToString:@"User_Id"]){
[tempDic setValue:tmpStr forKey:elementName];
NSLog([tempDic description]);
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
[AlertHandler hideAlert];
NSString *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"Login.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [tempDic valueForKey:@"User_Id"], nil] forKeys:[NSArray arrayWithObjects:@"userid", nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
if (plistData) {
[plistData writeToFile:DataPath atomically:YES];
}
nxthomeview = [[HomeView alloc]initWithNibName:@"HomeView" bundle:nil];
[self.navigationController pushViewController:nxthomeview animated:YES];
[nxthomeview release];
}
答案 0 :(得分:5)
首先,您必须将1-5-2010和7-5-2010解析为NSDate类型。您可以使用NSDateFormatter
:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSDate *date1 = [formatter dateFromString:@"1-5-2010"];
NSDate *date2 = [formatter dateFromString:@"7-5-2010"];
//difference between dates in miliseconds
NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
//interval can be positive or negative, depending on the date1 and date2 values
获得的日期格式化程序的更多信息
目前的日期可以通过[NSDate date]
获得。
如果您想获取当前周,从日期开始的星期几(称为日期组件)等等,您可以查看Date and Time Programming Guide