NSDate从数据库返回错误的日期

时间:2011-12-20 11:30:04

标签: iphone objective-c nsdate

我有一个应用程序,用户可以订购一些项目,并通过确认订单的日期保存并插入sqlite数据库。

然后,用户可以看到订单列表和订单日期,并且有一个选项,用户可以删除订单,直到他们选择的任何日期。

直到这一部分都没有问题,当我选择要显示的订单时,数据库返回的日期是正确的。

当用户想要根据他们在日期选择器中选择的日期删除订单时会出现问题,此时我从订单对象获取的日期始终在正确的日期之前,例如:

  

- 订单日期是2011/12/20 14:24:23 +0000
   - 显示给用户的订单日期与上述相同    - 我想一起比较日期的订单日期是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];
    yourCartViewController.title=@"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:我也在其他地方(订单清单)向客户显示其日期,并且日期中的日期是正确的。


编辑:很好的抓住Hot Licks,我到达的值是与订单相关的信息,当我分配日期{{1}时,问题出现在下面的代码中} NSString使用NSDate

dateFromString

[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显示前一天。我以前从未遇到过这样的事情,任何想法? :)

1 个答案:

答案 0 :(得分:0)

我想知道你的日期格式是否覆盖了最后的+0000。尝试类似[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];在解析之前。