2011-12-20 55 views
0

我有一個應用程序,用戶可以訂購一些項目,並通過確認訂單的日期訂單被保存並插入sqlite數據庫。NSDate從數據庫返回錯誤的日期

然後稍後用戶可以看到訂單清單和訂單日期,並有一個選項,用戶可以刪除訂單,直到他們選擇的日期。

直到這部分沒有問題,當我選擇要顯示的訂單時,數據庫返回的日期是正確的。

的問題,當用戶想要刪除基於他們在日期選擇器選擇日期的訂單,在這一點上,我從我的訂單對象獲得的日期始終是正確的日期前,如發生:

按訂單日期2011/12/20 14時24分23秒0000
顯示用戶-the訂單日期是與上述相同的
-the訂單日期時,我想比較日期一起2011/12/19 14:24:23 +0000

有什麼想法嗎?

編輯:

#import "OrderListViewController.h" 

@implementation OrderListViewController 

@synthesize newOrderTabelView, transferedOrderTableView, salesOrderArr; 
@synthesize newOrderTableViewController, transferedOrderViewController,salesOrder; 
@synthesize newOrderTableViewHeader, transferedOrderTableViewHeader, datePicker,isPickerHidden; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     isPickerHidden=YES; 
     [self initializeTableData]; 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [newOrderTabelView release]; 
    [transferedOrderTableView release]; 
    [newOrderTableViewController release]; 
    [transferedOrderViewController release]; 
    [salesOrderArr release]; 
    [newOrderTableViewController release]; 
    [transferedOrderViewController release]; 
    [salesOrder release]; 
    [datePicker release]; 
    [super dealloc]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 
-(void)editSaleOrderForEditButton 
{ 
    YourCartViewController *yourCartViewController=[[YourCartViewController alloc] initWithNibName:@"YourCartViewController" bundle:nil]; 
    [email protected]"Felton"; 
    [self.navigationController pushViewController:yourCartViewController animated:YES]; 
    [yourCartViewController release]; 
} 
-(IBAction)deleteOrderButton_Tapped 
{ 
    if(isPickerHidden) 
    { 
     datePicker.hidden=NO; 
     isPickerHidden=NO; 
    } 
    else if(!isPickerHidden) 
    { 
     NSDate* pickerDate = [datePicker date]; 
     datePicker.hidden=YES; 
     isPickerHidden=YES; 
     for(SalesOrder* order in salesOrderArr) 
    { 
     NSDate* orderDate = order.Date; 
     /*if([orderDate earlierDate:pickerDate]) 
     { 
      order.isDeleted = YES; 
     }*/ 
     switch([orderDate compare:pickerDate]) 
     { 
      case NSOrderedAscending: 
       order.isDeleted = YES; 
       break; 
      case NSOrderedSame: 
       order.isDeleted = YES; 
       break; 
      case NSOrderedDescending:break; 
     } 
     KDSDataSalesOrder* tSaleOrder = [[KDSDataSalesOrder alloc] init]; 
     [tSaleOrder updateSaleOrderIsDeleted:order]; 
     [tSaleOrder release]; 
    } 
    [self reloadNewOrderTable]; 
} 
} 
-(void)initializeTableData 
{ 
    KDSAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 
    KDSDataSalesOrder *dataSalesOrder=[[KDSDataSalesOrder alloc] init]; 
    NSArray* tNewSalesOrderArr=[dataSalesOrder selectSalesOrdersforSalesPerson:appDelegate.loggedInSalesPerson IsUploaded:NO IsDeleted:NO]; 
    self.salesOrderArr=tNewSalesOrderArr; 
    [dataSalesOrder release]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    datePicker.hidden = YES; 
    [super viewDidLoad]; 
    if(newOrderTableViewController == nil) 
    { 
     NewOrderTableViewController *tNewOrderTableViewController = [[NewOrderTableViewController alloc] init]; 
    self.newOrderTableViewController = tNewOrderTableViewController; 
    self.newOrderTableViewController.orderListViewController=self; 
    [tNewOrderTableViewController release]; 
} 
if(transferedOrderViewController == nil) 
{ 
    TransferedOrderTableViewController *tTransferedOrderTableViewController = [[TransferedOrderTableViewController alloc] init]; 
    self.transferedOrderViewController = tTransferedOrderTableViewController; 
    [tTransferedOrderTableViewController release]; 
} 

[newOrderTabelView setDataSource:self.newOrderTableViewController]; 
[newOrderTabelView setDelegate:self.newOrderTableViewController]; 

[transferedOrderTableView setDataSource:self.transferedOrderViewController]; 
[transferedOrderTableView setDelegate:self.transferedOrderViewController]; 

self.newOrderTableViewController.view = self.newOrderTableViewController.tableView; 
self.transferedOrderViewController.view = self.transferedOrderViewController.tableView; 

UIView *NewOrdeertableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(newOrderTabelView.frame.origin.x, newOrderTabelView.frame.origin.y-30, self.newOrderTabelView.frame.size.width, 30)]; 
NewOrdeertableHeaderView.backgroundColor = [UIColor colorWithRed:0.0196 green:0.513 blue:0.949 alpha:1.0]; 

UILabel *NewOrderTableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.newOrderTabelView.frame.size.width/2, 21)]; 
NewOrderTableHeaderLabel.text = @"New Order"; 
NewOrderTableHeaderLabel.backgroundColor = [UIColor clearColor]; 
NewOrderTableHeaderLabel.textColor = [UIColor whiteColor]; 
NewOrderTableHeaderLabel.textAlignment = UITextAlignmentLeft; 
[NewOrdeertableHeaderView addSubview:NewOrderTableHeaderLabel]; 
newOrderTableViewHeader = NewOrdeertableHeaderView; 
[self.view addSubview:newOrderTableViewHeader]; 

UIView *TransferedOrdertableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(transferedOrderTableView.frame.origin.x, transferedOrderTableView.frame.origin.y-30, self.transferedOrderTableView.frame.size.width, 30)]; 
TransferedOrdertableHeaderView.backgroundColor = [UIColor colorWithRed:0.0196 green:0.513 blue:0.949 alpha:1.0]; 

UILabel *TransferedOrderTableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.transferedOrderTableView.frame.size.width/2, 21)]; 
TransferedOrderTableHeaderLabel.text = @"Transfered Order"; 
TransferedOrderTableHeaderLabel.backgroundColor = [UIColor clearColor]; 
TransferedOrderTableHeaderLabel.textColor = [UIColor whiteColor]; 
TransferedOrderTableHeaderLabel.textAlignment = UITextAlignmentLeft; 
[TransferedOrdertableHeaderView addSubview:TransferedOrderTableHeaderLabel]; 
transferedOrderTableViewHeader = TransferedOrdertableHeaderView; 
[self.view addSubview:transferedOrderTableViewHeader]; 

[NewOrdeertableHeaderView release]; 
[NewOrderTableHeaderLabel release]; 
[TransferedOrdertableHeaderView release]; 
[TransferedOrderTableHeaderLabel release]; } 
-(void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 
    } 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 
-(void)reloadNewOrderTable 
{ 
    [self initializeTableData]; 
    [self.newOrderTableViewController initializeTableData]; 
    [newOrderTabelView reloadData]; 
} 
@end 

對不起,如果代碼被搞砸。在

NSDate* orderDate = order.Date; 

其中爲了來自SalesOrder它有一個NSDate在它出現問題,這NSDate的是在客戶確認訂單另一個地方設置,事情是當客戶確認訂單,日期這將被保存在我的SalesOrder是正確的,但是當我試圖在上面的代碼中分配它,它會在1天后...可能我犯了一個錯誤,並在這裏錯過了一些簡單的事實,幫助。

P.S:我還在其他地方(訂單列表)顯示客戶的日期,以及日期是否正確。


編輯:尼斯抓熱點舔,我得到的值有相關訂單信息,看來問題出在下面的代碼,當我使用dateFromString指定日期NSStringNSDate

[NSDateFormatter *formatter=[[NSDateFormatter alloc] init];  
[formatter setDateFormat:@"YYYY/MM/dd hh:mm:ss"];     
NSString *orderDateStr=[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)];  
NSDate *orderDate=[formatter dateFromString:orderDateStr]; 

orderDateStr顯示的順序正確的日期,但orderDate前一天表示。我從來沒有遇到過這樣的事情,有什麼想法? :)

+1

發佈您正在獲取/創建訂單對象的某些代碼。 – 2011-12-20 11:50:09

+2

我希望你不介意我已經刪除了你的問題的前兩行(我是新的,感謝你的幫助,我有這種情況)。如果沒有這兩行,在概覽頁面上查看問題的重要部分會更容易。歡迎來到stackoverflow。 – 2011-12-20 11:52:35

+0

尼克,我一回到家就會發布一些代碼。 – 2011-12-20 12:19:06

回答

0

我不知道你的日期格式是否覆蓋了+0000在end.Try東西像[格式化setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];解析之前。

+0

歡呼隊友:)再次感謝。我會確保在以後給你+1的時候可以:) – 2011-12-23 15:31:00