nsdate

How do you calculate the day of the year for a specific date in Objective-C?

早过忘川 提交于 2019-11-26 05:36:32
问题 This is something I found myself spending hours to figure out and therefore want to share with you. The question was: How do I determine the day of the year for a specific date? e.g. January 15 is the 15th day and December 31 is the 365th day when it\'s not leap year. 回答1: Try this: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];

Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat

我怕爱的太早我们不能终老 提交于 2019-11-26 05:20:52
问题 What setDateFormat option for NSDateFormatter do I use to get a month-day\'s ordinal suffix? e.g. the snippet below currently produces: 3:11 PM Saturday August 15 What must I change to get: 3:11 PM Saturday August 15 th NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [dateFormatter setDateFormat:@\"h:mm a EEEE MMMM d\"]; NSString *dateString = [dateFormatter

get NSDate today, yesterday, this Week, last Week, this Month, last Month… variables

那年仲夏 提交于 2019-11-26 04:59:45
问题 I am trying to do is to get NSDate today, yesterday, this Week, last Week, this Month, last Month variables ready for comparison for headers to be added on UITableView\'s titleForHeaderInSection What I want is done manually in the code below for date 2009-12-11 NSDate *today = [NSDate dateWithString:@\"2009-12-11 00:00:00 +0000\"]; NSDate *yesterday = [NSDate dateWithString:@\"2009-12-10 00:00:00 +0000\"]; NSDate *thisWeek = [NSDate dateWithString:@\"2009-12-06 00:00:00 +0000\"]; NSDate

Unexpected value from NSDate

[亡魂溺海] 提交于 2019-11-26 04:28:20
问题 I\'m trying to store a time in a date. In this case 1.5 seconds. I store the time in the date like this: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@\"mm:ss.SS\"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; NSDate *date1 = [dateFormatter dateFromString:@\"00:01.50\"]; But if I look in the debug area I can see that the value of date1 was set to: date1 = (NSDate *) 0x1dd32b30 2000-01-01 01:00:01 CET Could anyone

returns a date an hour in the future

删除回忆录丶 提交于 2019-11-26 04:27:32
问题 I\'m getting the current date/time using [NSDate date] . The value returned is an hour in the future. I\'ve check my phones location & time settings and they are correct. I can display the correct date and time as a string using the code below. [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle] But I need it to return the correct date/time as a date object as I use it to calculate the estimated time of arrival using

Convert string with unknown format (any format) to date

两盒软妹~` 提交于 2019-11-26 04:27:30
问题 I have data that contains a date string. Normally it would be in a \'Jan. 3, 1966\' type format, but because of international differences, it may not always be exactly that. I need to read the data in and convert it into a standard date string (\'YYYY-MM-DD\'). Basically this is what I have so far: var dataString = \'Jan. 3, 1966\' var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = # I DON\'T KNOW THE EXACT INPUT FORMAT ! let dateValue = dateFormatter.dateFromString(dataString)

Get NSDate from NSDate adjusted with timezone

自古美人都是妖i 提交于 2019-11-26 04:25:00
问题 I have a NSDate object. Let\'s say it represents \"1-10-2011\" NSDate *date = [df dateFromString:@\"2011-10-01 00:00:00\"]; That date translates into \"2011-09-30 22:00:00\" because of my timezone. Question: How do I get a new Date object representing \"2011-10-01 00:00:00\" in my local timezone? 回答1: NSDate only represents an absolute point in time . It has no concept of timezone or calendar. When you create a NSDate instance it is just a number of seconds since January 1st 2001 GMT! It does

How do you create a swift Date object

随声附和 提交于 2019-11-26 03:47:57
问题 How do you create a date object from a date in swift xcode. eg in javascript you would do: var day = new Date(\'2014-05-20\'); 回答1: Swift has its own Date type. No need to use NSDate . Creating a Date and Time in Swift In Swift, dates and times are stored in a 64-bit floating point number measuring the number of seconds since the reference date of January 1, 2001 at 00:00:00 UTC. This is expressed in the Date structure. The following would give you the current date and time: let

How to parse a date string into an NSDate object in iOS?

你说的曾经没有我的故事 提交于 2019-11-26 03:36:18
问题 I am trying to parse a date string from xml into an NSDate object in an iPhone app I am aware this must have been asked before, however, I believe I have the right syntax, but it is not working. Is there a problem with my code? The date string I need to parse is: 2011-01-21T12:26:47-05:00 The code I am using to parse it is: self.dateFormatter = [[NSDateFormatter alloc] init]; [self.dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [self.dateFormatter setLocale:[[[NSLocale

iOS: Compare two dates

泄露秘密 提交于 2019-11-26 02:51:53
问题 I have a NSDate that I must compare with other two NSDate and I try with NSOrderAscending and NSOrderDescending but if my date is equal at other two dates? Example: if I have a myDate = 24/05/2011 and other two that are one = 24/05/2011 and two 24/05/2011 what can I use? 回答1: According to Apple documentation of NSDate compare: Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date. - (NSComparisonResult)compare:(NSDate *)anotherDate