标签: nsdate

如何在iOS中将日期字符串解析为NSDate对象?

我试图将日期字符串从xml解析为iPhone应用程序中的NSDate对象

我知道之前一定要问过,但是,我相信我有正确的语法,但它不起作用.我的代码有问题吗?

我需要解析的日期字符串是:

2011-01-21T12:26:47-05:00
Run Code Online (Sandbox Code Playgroud)

我用来解析它的代码是:

self.dateFormatter = [[NSDateFormatter alloc] init];
    [self.dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [self.dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    [self.dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];



... 

else if([elementName isEqualToString:kUpdated]){
    self.currentQuestion.updated = [self.dateFormatter dateFromString:self.currentParsedCharacterData ];
}
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢.谢谢!

**基于来自ChrisKent的链接参考,我修复了这样的问题:

else if([elementName isEqualToString:kLastOnDeck]){

    NSString *dateStr = self.currentParsedCharacterData;
    // we need to strip out the single colon
    dateStr = [dateStr stringByReplacingOccurrencesOfString:@":" 
                                                 withString:@"" 
                                                    options:0 
                                                      range:NSMakeRange([dateStr length] - 5,5)];




    self.currentQuestion.lastOnDeck = [dateFormatter dateFromString:dateStr];

}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsdate nsdateformatter ios

48
推荐指数
1
解决办法
4万
查看次数

如何使用Swift获取当天的时间?

我怎么能在斯威夫特那里得到一天的时间.我曾尝试NSCalendarNSDateComponents,但我怕我刚开始用斯威夫特.

nsdate ios swift

48
推荐指数
3
解决办法
4万
查看次数

如何在Swift iOS中将字符串转换为日期字符串?

我正在学习swift,并且将日期String转换为NSDate转换为字符串.我以这种格式获取日期字符串"星期四,2015年10月22日07:45:17 +0000".我需要以MM-dd-yyyy格式显示日期.我尝试了以下代码,但它返回"null".

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
let dateObj = dateFormatter.dateFromString(dateString!)
print("Dateobj: \(dateObj)")
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助在哪里出错?期待着帮助.提前致谢.

string nsdate nsdateformatter ios swift

48
推荐指数
3
解决办法
10万
查看次数

两个日期之间的差异,以秒为单位

我有一个应用程序,其中显示内容给用户.我现在想知道用户实际查看该内容的秒数.所以在我的头文件中,我已经声明了一个

 NSDate *startTime;
 NSDate *endTime;
Run Code Online (Sandbox Code Playgroud)

然后在我的viewWillAppear中

 startTime = [NSDate date];
Run Code Online (Sandbox Code Playgroud)

然后在我的viewWillDisappear中

endTime = [NSDate date];
NSTimeInterval secs = [endTime timeIntervalSinceDate:startTime];
NSLog(@"Seconds --------> %f", secs);
Run Code Online (Sandbox Code Playgroud)

但是,应用程序崩溃,有时会出现不同的错误.有时它是内存泄漏,有时它是NSTimeInterval的问题,有时它会在第二次返回内容后崩溃.

有任何想法来解决这个问题吗?

nsdate nstimeinterval ios

47
推荐指数
2
解决办法
3万
查看次数

有没有一种简单的方法将ISO8601时间戳转换为格式化的NSDate?

如果我使用以下代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];   
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
NSDate *myDate = [dateFormatter dateFromString:@"2010-01-28T15:22:23.863"];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);
Run Code Online (Sandbox Code Playgroud)

它成功地转换为一个Date对象,但是,我似乎无法以任何其他方式格式化它yyyy-MM-dd'T'HH:mm,即记录的是什么2010-01-28T15:22:23

如果我更改dateFormat以说[dateFormatter setDateFormat:@"yyyy-MMMM-d'T'HH:mm"];Date对象为null ...

所以我的最终问题是如何格式化SQL数据库中的ISO8601时间戳,以便使用,例如,NSDateFormatterMediumStyle返回"2010年1月1日"?

objective-c iso8601 nsdate nsdateformatter

46
推荐指数
6
解决办法
3万
查看次数

检查一个NSDate是否大于另一个

if (datStartDate > datEndDate) {
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用.我知道有一个isEqual,等等,但我怎么表现"大于"?

两者都有NSDate.

objective-c nsdate

45
推荐指数
4
解决办法
2万
查看次数

额外的75秒来自哪里?

在朱利安日计算器上编写一些单元测试时,我发现1847年12月2日之前的日期被NSDate错误地初始化.他们似乎有75秒加入.我找不到任何指向那个日期的东西(这是在格里高利历年截止之后).这是一个错误还是我没有遇到历史性的日历调整?

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDateComponents *dateComps = [NSDateComponents new];
        dateComps.year = 1847;
        dateComps.month = 12;
        dateComps.day    = 1;
        NSDate *d1 = [cal dateFromComponents:dateComps];
        NSLog(@"d1 = %@", d1);

        dateComps = [NSDateComponents new];
        dateComps.year = 1847;
        dateComps.month = 12;
        dateComps.day    = 2;
        NSDate *d2 = [cal dateFromComponents:dateComps];
        NSLog(@"d2 = %@", d2);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

d1 = 1847-12-01 00:01:15 +0000

d2 = 1847-12-02 00:00:00 +0000

objective-c nsdate nsdatecomponents

45
推荐指数
2
解决办法
5557
查看次数

如何检查两个NSDates是否来自同一天

我正在进行ios开发,我发现很难检查两个NSDates是否来自同一天.我试着用这个

   fetchDateList()
    // Check date
    let date = NSDate()
    // setup date formatter
    let dateFormatter = NSDateFormatter()
    // set current time zone
    dateFormatter.locale = NSLocale.currentLocale()

    let latestDate = dataList[dataList.count-1].valueForKey("representDate") as! NSDate
    //let newDate = dateFormatter.stringFromDate(date)
    let diffDateComponent = NSCalendar.currentCalendar().components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day], fromDate: latestDate, toDate: date, options: NSCalendarOptions.init(rawValue: 0))
    print(diffDateComponent.day)
Run Code Online (Sandbox Code Playgroud)

但它只是检查两个NSDates是否有24小时的差异.我认为有一种方法可以使它工作但是,我希望在早上凌晨2点之前将NSDate值计算为前一天,所以我在这里肯定需要一些帮助.谢谢!

nsdate ios swift

45
推荐指数
2
解决办法
2万
查看次数

Swift 3 - 查找两个日期之间的日历天数

我在Swift 2.3中这样做的方式是:

let currentDate         = NSDate()
let currentCalendar     = NSCalendar.currentCalendar()

var startDate : NSDate?
var endDate   : NSDate?

// The following two lines set the `startDate` and `endDate` to the start of the day

currentCalendar.rangeOfUnit(.Day, startDate: &startDate, interval: nil, forDate: currentDate)
currentCalendar.rangeOfUnit(.Day, startDate: &endDate, interval: nil, forDate: self)

let intervalComps = currentCalendar.components([.Day], fromDate: startDate!, toDate: endDate!, options: [])

print(intervalComps.day)
Run Code Online (Sandbox Code Playgroud)

现在这已经全部改变了Swift 3.我必须使用NSCalendarNSDate不断地输入as,或者找到Swift 3的方式.

在Swift 3中使用它的正确方法是什么?

nsdate nscalendar swift3

44
推荐指数
6
解决办法
4万
查看次数

UIDatePicker中的最小和最大日期

我想从日期选择器获取最小和最大日期,但最小日期应为当前日期的" - 18",最大日期应为当前日期的" - 100".

假设当前年份是2018年,那么我想要最小日期2000和最大日期1918.

到目前为止我所做的是:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];

NSInteger year = [components year];

int mindt = year - 18;

int maxdt = year -100;

   // NSDate * MinDate = [components year] - 18;

   // NSDate * MaxDate =  [components year] - 100;

   // self.datePicker.minimumDate = MinDate;

   // self.datePicker.maximumDate = MaxDate;
Run Code Online (Sandbox Code Playgroud)

但我不能得到这个整数到我的日期格式..

objective-c nsdate ios nsdatepicker

43
推荐指数
6
解决办法
7万
查看次数