nsdate

How to manage NSDate for different timezones

柔情痞子 提交于 2019-12-04 16:50:17
I have created a calendar in my app, using the date object this way: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]]; NSInteger day = [weekdayComponents day]; NSInteger month = [weekdayComponents month]; NSInteger year = [weekdayComponents year]; m_dateFormatter.dateFormat = @"dd/MM/yyyy"; [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; NSDateComponents

Get weekdays within a month

江枫思渺然 提交于 2019-12-04 16:46:11
I'm trying to get the dates within a given month. My plan is to Get the start and the end dates of a given month,. Get all the dates that fall within that range. Iterate through them and eliminate dates that fall within a weekend using the isDateInWeekend method. The remaining dates are the weekdays. So I created two NSDate extension methods to get the start and the end dates of the month. extension NSDate { var startOfMonth: NSDate { let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Year, .Month], fromDate: self) return calendar.dateFromComponents(components)!

NSDateFormatter giving me time 4 hours ahead

核能气质少年 提交于 2019-12-04 16:42:19
I'm converting a string "Jun 11, 2012 9:30 PM" to an NSDate and I keep getting 4 hours ahead for some reason. The funny thing is I'm using this same string to feed a UIDatePicker in a detailed view where I have to do the same conversion, and the UIDatePicker renders the time fine. Only when I now try to NSLog it in my main and detailed view do I have problems. This is in my views : NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MMMM dd, yyyy h:mma"]; NSDate *dateFromString = [[[NSDate alloc] init]autorelease]; NSLog(@"DATE %@", _date); dateFromString =

How to format time intervals for user display (social network like) in swift?

送分小仙女□ 提交于 2019-12-04 15:59:14
问题 I have a time interval, say, 12600, which is equivalent to 3 hours 30 minutes. How could I format any such time interval so that only the highest part of the interval (for example in this case figure, the hours) is kept and have the correct locale abbreviation be appended to the number. For example 10m (10 minutes), 3d (3 days), 1y (1 years). EDIT: Here are some examples: Time interval in: 90000 Whole string: 1d String out: 1d Time interval in: 900 Whole string: 15m String out: 15m Time

how to use NSDateFormatter to see “Today” string

那年仲夏 提交于 2019-12-04 15:45:58
问题 apple use it in the messages app for today messages Today 11:45 AM Yesterday 11:45 AM i see it in the apple developer site To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35. The version of the standard varies with release of the operating system: Calendar Fields fields ( alias | (field*, special*)) > field ( alias | (displayName?, relative*, special*)) > Translations may be supplied

Producing an NSDate with NSDateFormatter Yielding nil NSDate Value

余生颓废 提交于 2019-12-04 15:42:39
I am trying to produce a date from a string with the following: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMMM dd, yyyy HH:mm:ss"]; NSLog(@"ENDINGDATE: %@", [closure objectForKey:@"ENDINGDATE"]); NSDate *tempDate = [dateFormatter dateFromString:[closure objectForKey:@"ENDINGDATE"]]; The ENDINGDATE NSLog produces the following: ENDINGDATE: April, 21 2011 20:00:00 Everything seems to be fine, so I am stumped as to why tempDate is being set to nil. Any ideas? Thanks! The , is misplaced. It should be after MMMM . So the format should have

Difference in hours between two NSDate

故事扮演 提交于 2019-12-04 15:19:14
问题 Here I'm trying to calculate the hours between two dates. When i run the application, it crashes. Could you please tell me the mistake in this code? NSString *lastViewedString = @"2012-04-25 06:13:21 +0000"; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"]; NSDate *lastViewed = [[dateFormatter dateFromString:lastViewedString] retain]; NSDate *now = [NSDate date]; NSLog(@"lastViewed: %@", lastViewed); /

Show time like a digital clock

流过昼夜 提交于 2019-12-04 14:59:47
I am able to display the current time on my iPad application using the code, NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle: NSDateFormatterShortStyle]; NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]]; timeLabel.text = currentTime; But this gives the time only when the application is loaded. How do i get the time to keep running? Like a digital clock. Use this: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle: NSDateFormatterShortStyle]; [NSTimer scheduledTimerWithTimeInterval:1.0

How to get dates of current month in Objective c?

余生颓废 提交于 2019-12-04 14:46:35
I am developing a app which uses the date, month, year. I want the current month dates,because current month can have 28,29,30,31 days. I have tried to get current year months. but i don't know the code for above. here is my code. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *today = [NSDate date]; NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDateComponents *yearComponents = [currentCalendar components:NSYearCalendarUnit fromDate:today]; int currentYear = [yearComponents year]; for(int months = 0; months < 12; months++) { NSString *monthName =

How to write NSData to a new file in Swift?

亡梦爱人 提交于 2019-12-04 14:43:12
I am working to get the contents of an NSData instance written to a file. I am currently using an Xcode playground. This is my code: let validDictionary = [ "numericalValue": 1, "stringValue": "JSON", "arrayValue": [0, 1, 2, 3, 4, 5] ] let rawData: NSData! if NSJSONSerialization.isValidJSONObject(validDictionary) { do { rawData = try NSJSONSerialization.dataWithJSONObject(validDictionary, options: .PrettyPrinted) try rawData.writeToFile("newdata.json", options: .DataWritingAtomic) } catch { // Handle Error } } I have a file named newdata.json located in resources but when I check it there is