nsdate

NSDate from NSString gives null result

核能气质少年 提交于 2019-12-19 12:53:25
问题 I am using following code to generate NSDate -> NSString +(NSString *)getCurrentTime { NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString* str =[dateFormatter stringFromDate:now]; [dateFormatter release]; NSLog(@"%@",str); return str; } everything is fine in above code. I am using above code to store string in

NSDate from NSString gives null result

自作多情 提交于 2019-12-19 12:53:09
问题 I am using following code to generate NSDate -> NSString +(NSString *)getCurrentTime { NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString* str =[dateFormatter stringFromDate:now]; [dateFormatter release]; NSLog(@"%@",str); return str; } everything is fine in above code. I am using above code to store string in

Comparing time in NSDate

一笑奈何 提交于 2019-12-19 11:54:01
问题 I am trying to compare the time from two NSDate objects but am getting the wrong answer. I think it is because when using the NSDate compare methods it is also including the date. I just want to ignore the date and compare the times. How can this be done? 回答1: well, you can always use NSCalendar's - (NSDate *)dateFromComponents:(NSDateComponents *)comps 来源: https://stackoverflow.com/questions/3773279/comparing-time-in-nsdate

init] returning date an hour in the past?

落爺英雄遲暮 提交于 2019-12-19 11:31:41
问题 When I call NSDate *now = [[NSDate alloc] init]; in order to get the current date and time, I check it with: NSLog(@"Date now: %@", now); the date outputted is one hour in the past. 2010-10-08 12:04:38.227 MiniBf[1326:207] Now: 2010-10-08 11:04:38 GMT Is my time zone set incorrectly somewhere perhaps? Thanks! Michael 回答1: Use NSDateFormatter to localize the date: NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle

I get nil when using NSDateFormatter in swift

本小妞迷上赌 提交于 2019-12-19 11:29:48
问题 This is my code : println(myStringDate) // 2015-02-26T17:46:34.000Z let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "YYYY-MM-DDTHH:mm:ss.sssZ" let myDate = dateFormatter.dateFromString(myStringDate) println(myDate) // nil Why nil ? What am I doing wrong ? 回答1: Your problem is how you set the date format. Because there is a T in your string, you need to "escape" it and say, that it shouldn't affect the formatting of the date. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss

init] returning date an hour in the past?

不想你离开。 提交于 2019-12-19 11:29:15
问题 When I call NSDate *now = [[NSDate alloc] init]; in order to get the current date and time, I check it with: NSLog(@"Date now: %@", now); the date outputted is one hour in the past. 2010-10-08 12:04:38.227 MiniBf[1326:207] Now: 2010-10-08 11:04:38 GMT Is my time zone set incorrectly somewhere perhaps? Thanks! Michael 回答1: Use NSDateFormatter to localize the date: NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle

Why can't NSDate be compared using < or >?

痞子三分冷 提交于 2019-12-19 10:18:05
问题 NSDate *date = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"MM/dd/yyyy"]; NSLog([@"today is " stringByAppendingString:[dateFormat stringFromDate:date]]); NSLog([@"firstBirthdayDate is " stringByAppendingString:[dateFormat stringFromDate:firstBirthdayDate]]); NSLog([@"secondBirthdayDate is " stringByAppendingString:[dateFormat stringFromDate:secondBirthdayDate]]); if ([firstBirthdayDate isEqualToDate:secondBirthdayDate]) NSLog(@"First

dateFromString return nil after change 24 hour

做~自己de王妃 提交于 2019-12-19 10:12:24
问题 NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"hh:mm a"]; NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; if(sourceDate==nil) { NSLog(@"source date nil"); } I'm using 24 hour setting in iPhone. After I change to 24 hour setting, datefromstring always return nil. I rechange to 12 hour format, it's working again. Why ? 回答1: The locale needs to be set to avoid being affected by the 24-hour setting change. The format also has to

Convert date to timestamp in iOS

女生的网名这么多〃 提交于 2019-12-19 09:57:26
问题 How do you convert any given date to milliseconds? For example, 2014-01-23 to timestamp conversion. NSDate *date = [dateFormatter dateFromString:@"2014-01-23"]; NSLog(@"date=%@",date); NSTimeInterval interval = [date timeIntervalSince1970]; NSLog(@"interval=%f",interval); NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval]; [dateFormatter setDateFormat:@"yyyy/mm/dd "]; NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]); Output result: 1970/30/01 回答1: Have it a

NSDateFormatter issue with date format in iOS 8

半世苍凉 提交于 2019-12-19 09:42:16
问题 I have an NSDateFormatter which I use to format an NSDate into a string. The following format does not work: ddMMyyyy_hhmmss_SSS. When I try to format an NSDate I get the following output: 18092014_08:49:03,638, which has a colon and a comma I didn't specify in the format. Note that in iOS 7 I never had this kind of problem. This is the code I'm using: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat: @"ddMMyyyy_hhmmss_SSS"]; NSString *dateString =