将字符串格式的日期转换为字符串日期前一天的 nsdate 结果 [英] Converting a date by string format to nsdate result one day before string date

查看:45
本文介绍了将字符串格式的日期转换为字符串日期前一天的 nsdate 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了我的问题,但没有找到任何相关的帮助...

I searched about my problem but I did not find any help related...

故事:用户通过名称、开始日期、结束日期和注释创建一个继续事件,因此将所有字段保存在 Sqlite 数据库中.两种日期格式都是yyyy-MM-dd".之后,当用户想要对事件执行操作时,应用会检查用户通过开始和结束日期选择的日期,如果输入的日期早于开始日期或结束日期,则会通知用户.

The story: The user create a continues event by name, start date, end date and note, so saves all fields on the Sqlite database. The both dates format are "yyyy-MM-dd". After that, when user want to do an action on event, the app checks the date that user selected by start and end date and if the entered date is before start date or after end date notifies user.

问题:我作为用户创建了一个从 18-8-2012 到 18-9-2012 的事件.然后我在 18-8-2012 上登录并收到通知.我调试了应用程序.我很惊讶.格式为yyyy-MM-dd"的字符串中的日期对象是2012-08-17 19:30:00 +0000".代码是:

The problem: I, as an user, created an event from 18-8-2012 to 18-9-2012. Then I put log on 18-8-2012 and I was notified. I debugged app. and I was amazed. the date object from string with format "yyyy-MM-dd" is "2012-08-17 19:30:00 +0000". The code is:

           #define kPickerDate @"yyyy-MM-dd"
           NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
           [formatter setDateFormat:kPickerDate];
           NSDate *sDate = [formatter dateFromString:start_date]; 
           NSDate *eDate = [formatter dateFromString:exam_date]; 

应用程序如何???????????????

How does app that??????????????

推荐答案

您可以使用更好的方法创建 NSDate 对象.首先修改 NSDate 的@interface(你可以在任何地方这样做).为此,请将其放在您的头文件中.

You can create an NSDate object using a better method. First amend the @interface for NSDate (you can do this anywhere). To do this, put this in your header file.

@interface NSDate (missingFunctions) 
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
@end

然后在主文件中放入:

@implementation NSDate (missingFunctions)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:year];
    [components setMonth:month];
    [components setDay:day];

    //BUT MOST IMPORTANTLY:
    [components setTimeZone:[NSTimeZone localTimeZone]];
    [components setHour:hour];
    [components setMinute:minute];
    [components setSecond:second];

    return [calendar dateFromComponents:components];
}   
@end

然后从代码调用中的任何地方(例如):

Then from anywhere in code call (for example):

[NSDate dateWithYear:1969 month:8 day:15];

它会根据您的请求在正确的时区返回一个 NSDate.您还可以通过阅读有关 NSTimeZone 在苹果官方文档中.

And it will return an NSDate as per your request in the right time zone. You can change which time zone to use also by reading more about NSTimeZone at the official apple docs.

注意我不能因为编写此代码而受到赞扬,但我可以保证它可以工作.

Note I can't take credit for writing this code, but I can vouch for it working.

这篇关于将字符串格式的日期转换为字符串日期前一天的 nsdate 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆