Menu
  • HOME
  • TAGS

Getting timezone offset from a date string

objective-c,nsdate,nsdateformatter

I've finally ended up using this code: NSString *dateStringWithTZ = @"2015-04-18T08:35:00.000+03:00"; // Create date object using the timezone from the input string. NSDateFormatter *dateFormatterWithTZ = [[NSDateFormatter alloc] init]; dateFormatterWithTZ.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ"; NSDate *dateWithTZ = [dateFormatterWithTZ dateFromString:dateStringWithTZ]; // Cut off the timezone and create date object using GMT timezone. NSString *dateStringWithoutTZ...

Convert long month name to int

swift,cocoa-touch,nsdate,nsdateformatter,date-parsing

Use MM for the month format. Use stringFromDate to convert your NSDate to a String. Then convert your string to an Int with .toInt() let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM" let monthString = dateFormatter.stringFromDate(NSDate()) // "05" let monthInt = monthString.toInt() // 5 ...

Converting NSDate to NSString and back won't return the same date

ios,ios8,nsdate,nsdateformatter

The problem is that you lose the sub-second accuracy in conversion. When creating [NSDate now], the sub-second information is kept in NSDate object. Then you convert it to string that does not keep that information, and after the conversion back it's simply gone. As the documentation for isEqualToDate: states: This...

time change after converting using NSDateFormatter

objective-c,nsdate,nsdateformatter

the time is read using your local timezone and the NSDate shown uses UTC time (as it always does when not using a specific date formatter you explicity tell it to use the local timezone [dateFormat setTimeZone:[NSTimeZone defaultTimeZone]]; //! NSDate itself though is a timestamp without any notion of timezones...

Localized NSDateFormatter

ios,objective-c,nsdateformatter

Your date format for 24 hours (uppercase H), just change it to use 12 hours (lowercase h): [dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"] You can find date format patters here...

How to display only the day and month of a date based on the locale?

ios,swift,nsdateformatter

Something along these lines, perhaps: let d = // the date let df = NSDateFormatter() let format = NSDateFormatter.dateFormatFromTemplate( "dMMMM", options:0, locale:NSLocale.currentLocale()) df.dateFormat = format let s = df.stringFromDate(d) Note that both language and region settings on the device are involved in the outcome....

NSString to NSDate doesn't work

ios,objective-c,nsdateformatter

You have to set the date format as the string NSString *myDate = @"06/18/2015 8:26:17 AM"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM/dd/yyyy h:mm:ss a"]; NSDate *date = [dateFormatter dateFromString:myDate]; //Set New Date Format as you want [dateFormatter setDateFormat:@"dd.MM. HH:mm"]; [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]]; NSLog(@"%@",[dateFormatter stringFromDate:date]); ...

NSDateFormatter dateFromString return wrong year

ios,nsdate,nsdateformatter

The issue is the locale of the formatter. If you want to use gregorian calendar regardless of the device settings, you generally should set locale to en_US_POSIX. formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; See Apple Technical Q&A #1480. This is geared towards RFC 3339/ISO 8601 dates, but it really applies anywhere you're...

Remove some part of formatted string from NSDateFormatterLongStyle

ios,nsdateformatter

Playground test: let lc = "zh-cn"; var locale = NSLocale(localeIdentifier: lc); var format = NSDateFormatter.dateFormatFromTemplate("yyyyMMM", options: 0, locale: locale); var formatter = NSDateFormatter(); formatter.dateFormat = format; formatter.locale = locale; formatter.stringFromDate(NSDate()); Production code: var format = NSDateFormatter.dateFormatFromTemplate("yyyyMMM", options: 0, locale: nil); var formatter = NSDateFormatter(); formatter.dateFormat = format; let dateString =...

NSDateFormatter returning wrong minutes

ios,nsdateformatter

Your date formatter is not right,try this one [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"]; And this will output 2015-05-19 10:31:04.530 OCTest[2428:76257] string 2015-05-19T01:48:33.603890Z 2015-05-19 10:31:04.534 OCTest[2428:76257] dateOfCall 2015-05-19 01:48:33 +0000 2015-05-19 10:31:35.113 OCTest[2428:76257] dateString 9:48 AM I am not sure if this is what you want,if you have more questions,feel free to comment...

Format NSDate with different calendar identifier?

swift,nsdate,nsdateformatter,nsdatecomponents

Just add the following line formatter.calendar = calendar ...

Converting different strings to NSDate - 1 is correct, the 2nd is a duplicate of the 1st

objective-c,nsdate,nsdateformatter,ios8.1

It all comes down to knowing your format code strings. You are misusing "HH" here so the hour is coming out wrong. Another problem is that you are not capturing the error. By calling dateFromString:, you are missing out on your chance to hear about errors. Use getObjectValue:forString:range:error: to learn...

NSDateFormatter not working to covert NSString to NSDate

nsdateformatter

Wrong format, use HH instead of hh. The h directive is for hour in the am/pm format, will not work with 17. See this comprehensive date formatting guide...

Using NSTimeInterval after using dateFromString

swift,date,nsdate,nsdateformatter,nstimeinterval

This method only saves minutes. My suggestion is to save it as time interval var interval = NSDate().timeIntervalSince1970 And then load it up by adding that time to 1970 date like this var date = NSDate(timeIntervalSince1970: interval) ...

NSArray with week days

ios,objective-c,nsdate,nsdateformatter

Call this method to rationalize an array to "Weekdays" or "Weekend" if it contains the correct days. Else it will leave the array intact. It's easy to change to make it return a string instead, but I'll leave that to you. - (NSArray *)rationalizeDaysOfWeek:(NSArray *)daysOfWeek { if ([self array:daysOfWeek matchesArray:@[...

Objective-C: What is the date format for a date like 2014-02-02T20:55:31.555Z

ios,objective-c,cocoa,nsdate,nsdateformatter

You're missing the fractional seconds at the end of the date, you'll need to add them to your format string: yyyy-MM-dd'T'HH:mm:ss.SSS'Z' ...

NSDate value is always returned in my time zone

objective-c,nsdate,nsdateformatter

TLDR: [NSDate dateWithTimeIntervalSince1970:0] represents the date you're asking for. It is the conversion of that NSDate to a string that involves your time zone. As humans, we have many ways to represent an instant in time. For example, “January 1, 2001 00:00:00 GMT” represents a particular instant in time. Another...

NSDateFormatter dateFromString format string issue

ios,objective-c,nsdateformatter

You are missing a 'y' in your format. It should be: NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss"; return [formatter dateFromString:dateString] ...

NSDateFormatter gives back totally wrong value [duplicate]

ios,objective-c,nsdate,nsdateformatter

modify the date formatter YYYY to yyyy you can get exact values [dateFormatter setDateFormat:@"dd/MM/yyyy"]; yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically...

NSDateFormatter swapping between Style and Format

objective-c,cocoa,cocoa-touch,nsdateformatter

The date formatter uses the style if format is nil. So add the following line: df.dateFormat = nil; when you want to go back to use the style....

String to NSdate not working

ios,nsdate,nsdateformatter

The conversion is done in your local time zone, while simply printing a date it will be printed in GMT (hence "… +0000") NSLog(@"%@", [df stringFromDate:date]); This will print the date in local time zone...

SWIFT : Convert String from XLM to NSDate

ios,swift,nsstring,nsdate,nsdateformatter

If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system...

cannot invoke 'stringFromDate' error in swift

ios,swift,nsdateformatter

check the following code: var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" let d = NSDate() let s = dateFormatter.stringFromDate(d) println(s) ...

NSDateformatter: How to decide date formatter string

date,nsdateformatter,date-formatting

Try this: [dateFormatter setDateFormat:@"EEE-MMM-dd-hh:mm:ss-z-yyyy"]; The pattern is case sensitive. For more information see here...

parsing the date and getting the day off by 1, nsdate

ios,objective-c,nsdate,nsdateformatter,nsdatecomponents

It's due the difference between your timezone and UTC. The value 2029-12-31T00:00:00+00:00 is on December 31 in the UTC timezone. But when you setup the date components you are getting back the date in your timezone where it's December 30 local time.

Converting A NSDate Array to a String Array in Swift

string,swift,parse.com,nsdateformatter

This is a highly inefficient way of formatting the dates into a String since it instantiates a new NSDateFormatter each time. Consider creating a constant NSDateFormatter for the class as an optimisation. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell let formatter...

Parse date string with NSDateFormatter with month begins from 0

objective-c,cocoa,date,nsdateformatter

Try this: NSDateFormatter* formatter = [NSDateFormatter new]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; formatter.shortMonthSymbols = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11"]; formatter.dateFormat = @"yyyy MMM dd"; // Note 3 Ms for "short month" format NSDate* theDate = [formatter dateFromString:@"2002 0 20"]; Result: 2002-01-20 00:00:00 +0000 ...

Swift Date Formatting

swift,nsdateformatter

NSDateFormatter Class Reference : http://goo.gl/7fp9gl Date Formatting Guide (Apple) : http://goo.gl/8zRTQl A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. Your code should work, as you expect, like this : let dateStr...

Get current NSDate with a format of: dd/MM/yyyy

objective-c,nsdate,nsdateformatter

If you want to get current day like format this: dd/MM/yyyy. You can use this code: NSDate *datecenter = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"d, MMMM, YYYY"]; NSString *dateString = [dateFormat stringFromDate:datecenter]; lbldaymonthyear.text = dateString; ...

NSDateFormatter time format depending on Locale

ios,swift,nsdateformatter

You can use the localized string from date function like this... extension NSDate { func localizedStringTime()->String { return NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.ShortStyle) } } ...

Extracting time from NSDate in Swift

nsdate,nsuserdefaults,nsdateformatter

Retrieve the NSDate object from NSUserDefaults by calling punchTimes.objectForKey("punchInTime") Then use NSDateFormatter class to create a String formatted the way you want it....

NSDateFormatter for 2015-02-13 17:16:00 +0000

ios,nsdate,nsdateformatter

The error is clearly showing that value is already an NSDate, not an NSString. All of your code can be replaced with one line: NSDate *value = [newProperties objectForKey:key]; You can verify that value is a date by logging its class: NSLog(@"value class = %@", [value class]); As a side...

Issues in converting date string (any time zone) to NSDate of French Time Zone

ios,objective-c,nsdate,nsdateformatter,nstimezone

If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question. Because you have set the timezone of your parsing dateFormatter to Paris the string...

Swift: can't get NSDate dateFormatter right

ios,date,swift,nsdate,nsdateformatter

It looks like your date formatter is a bit off. "MM'/'DD'/'YYYY" reads "Padded month / day of the year (not month) / year of the current week (so Jan 1-6 could overlap and add side-effects)" I'm guessing that what you're aiming for is "MM'/'dd'/'yyyy", which reads "Padded month / padded...

Swift code with multiple NSDateFormatter - optimization

ios,swift,core-data,nsdateformatter

You can create only one instance on NSDateFormatter and use it where you need it: lazy var dateFormatter: NSDateFormatter = { let f = NSDateFormatter() f.locale = NSLocale(localeIdentifier: "en_US_POSIX") f.timeZone = NSTimeZone(abbreviation: "GMT+0:00") f.dateFormat = "dd-MM-yyyy" return f }() ...

Check if time of NSDate object has passed

ios,nsdate,nsdateformatter

To get the hour and minutes component, you do this: NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; You can then create helper methods to keep the code organized: - (NSInteger)hourFromDate:(NSDate *)date { return [[NSCalendar currentCalendar] component:(NSCalendarUnitHour) fromDate:date]; }...

CHANGE FORMAT TIME IN OBJECTIVE-C

ios,objective-c,nsdate,nsdateformatter

Please check below code - NSString *[email protected]"01:32 PM"; NSDateFormatter *formatHora = [[NSDateFormatter alloc] init]; [formatHora setDateFormat:@"hh:mm a"]; NSDate *dateHora = [formatHora dateFromString:hora]; [formatHora setDateFormat:@"HH:mm"]; NSString* horaFormatoCorrecto = [formatHora stringFromDate:dateHora]; ...

NSDateFormatter - Adding a Year? [duplicate]

ios,objective-c,xcode,nsdateformatter

yyyy specifies the calendar year, whereas YYYY specifies the year (of “Week of Year”) you must change your dateformatter style from [dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss zzz"]; to [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"]; for your reference, check this apple documentation...

Converting string to date with NSDateFormatter

ios,objective-c,nsstring,nsdateformatter

NSString *str = @"2015-05-08T09:44:25.343"; NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];// this string must match given string @"2015-05-08T09:44:25.343" NSDate *date = [dateFormat dateFromString:str]; NSLog(@"%@",date); [dateFormat setDateFormat:@"MM/dd/yyyy"];// this match the one you want to be NSString *convertedString = [dateFormat stringFromDate:date]; NSLog(@"%@", convertedString); ...

Trouble With NSDateFormatter TimeZone Format

ios,xcode,nsdateformatter

Modify your code as below. NSString *dateStr = @"2015-02-04T01:02:27.363Z"; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.zzzZ"]; NSDate *date = [dateFormat dateFromString:dateStr]; ...

NSDateFormatter returning null when trying to fetch date from string

ios,objective-c,nsdate,nsdateformatter

The format string you specified is not correct. The month and day fields are in wrong place (That need to be interchanged). You need to change that to: [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; ...

NSDateFormatter adds 10hours?

ios,nsdate,nsdateformatter

The problem is that dateFromString: depends on the timezone of your NSDateFormatter. So you need to tell your formatter, which timezone to use: NSString *serverDateString = array[0]; NSString *nextEventDateString = array[1]; NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"hh:mm dd.MM.yyyy"]; // Set the time zone here [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CET"]]; // If...

How to convert arbitrary time to NSDate to format it using NSDateFormatter?

ios,objective-c,xcode,nsdate,nsdateformatter

Use following code, NSString *string = @"15:00"; NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init]; [inputDateFormatter setDateFormat:@"HH:mm"]; NSDate *date = [inputDateFormatter dateFromString:string]; NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init]; [outputDateFormatter setDateFormat:@"hh:mm a"]; NSString *outputString = [outputDateFormatter stringFromDate:date]; ...

Swift - convert date string from a “datepicker filled date field” to nsdate

swift,nsdate,nsdateformatter,uidatepicker

I think I've fixed it. I think the thing that was causing problem was that I was setting locale to currentLocale but then 2 lines down specifying a dateFormat which would work if it corresponds to the current locale but not otherwise. Anyway, fix appears to be just specify locale...

Nsdateformatter with nsdate for getting exact date in iOS

ios,nsdateformatter

See: ICU Formatting Dates and Times Also: Date Field SymbolTable., look specifically at "Z". Z Time Zone: ISO8601 basic hms? / RFC 822 -0800 Example: NSString *dateString = @"Fri, 06 Feb 2015 11:25:00 -0600"; NSLog(@"dateString: %@", dateString); Input dateString: dateString: Fri, 06 Feb 2015 11:25:00 -0600 NSDateFormatter *formatter = [NSDateFormatter...

Modify date format of a string

swift,nsdateformatter

.dateFromStringcreates an NSDate() from a String. The format of the string has to be set with .dateFormat You set the format to "d MMMM" but your myDate variable does not conform to that format. I believe you want to make a String in your "d MMMM" format out of a...

NSDateFormatter dateFormat ShortStyle not working

ios,iphone,swift,xcode6,nsdateformatter

My friend looked at the code and point out the problem. This problem is caused by the differences of the date format between Chinese/Japanese and English. In Chinese/Japanese, the date format is year/month/date. But in English, the date format is month/date/year. In my code, I set the dateStyle into ShortStyle...

NSDateFormatter- stringFromDate not returning AM/PM

ios,datetime,nsdate,nsdateformatter,date-format

Check your device settings for 24 hour time. If it is on, turn it off. Your code is perfectly fine. I am getting output as shown below : Today's Time: 04:31:58 PM If you don't want to show seconds use this code : [formatter setDateFormat:@"hh:mm a"]; ...

IOS: Get current date/time with specific (non-localized) format

ios,objective-c,iphone,string-formatting,nsdateformatter

You need to use HH instead of hh in the date formatting specifier: @"yyyy-MM-dd_HH_mm_ss" ^^ ...

Adding Time Offset in Swift

swift,nsdateformatter,timezoneoffset

If you want to convert a show time which is stored as a string in GMT, and you want to show it in the user's local time zone, you should not be manually adjusting the NSDate objects. You should be simply using the appropriate time zones with the formatter. For...

How to get system time format when region is “United Kingdom” in ios?

ios,objective-c,iphone,nsdateformatter,date-formatting

After lot of r&d on this topic finally i find the solution for this issue. This uses a special date template string called "j". According to the ICU Spec, "j"... Requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K,...

Getting wrong date on changing date format

ios,ios7,ios8,nsdate,nsdateformatter

You want to be using yyyy instead of YYYY. The small y is the calendar year, whereas the capital Y is the year of the "week of year". Below is an explanation from the specification. F.4 Week of Year Values calculated for the Week of Year field range from 1...

Format NSDate to this format in NSString:- January 15 2013

ios,objective-c,iphone,nsdate,nsdateformatter

The problem with your code is that your are initialising the NSDateComponents with day 1, and since you are not providing any year, it is taking year as "1". So for your code to work properly, you may set the year of the current date as the NSDateComponents' year. NSCalendar...

How to detect the system time format change in ios?

ios,iphone,nsdate,nsdateformatter,nslocale

I'm not sure why you expect that the dateFormat changes if the date changes significantly. The significant time change event is triggered when the time (i.e. [NSDate date]) changes. For example if a new day starts, if the user changes timezone or if daylight-saving starts or ends. But those events...

How to get weekends vs weekdays in Cocoa?

cocoa,locale,nsdateformatter,nscalendar

I finally got it. The mistake was in the locale identifier which should be en_AE instead of en_AR import UIKit let arabicLocale = NSLocale(localeIdentifier: "en_AE") let islamicTabularCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicTabular) islamicTabularCalendar?.locale = arabicLocale let islamicTabularFormatter = NSDateFormatter() islamicTabularFormatter.calendar = islamicTabularCalendar islamicTabularFormatter.dateStyle = NSDateFormatterStyle.FullStyle let gregorianCalendar = NSCalendar(calendarIdentifier:...

String to NSdate returns nil

ios,swift,datetime,nsdateformatter

Besides the problem that the string you were testing had milliseconds while the date from your array has only seconds, now the problem is that Y is for week of year. You need to use lowercase "y" with the date format and also lowercase "d". The date format should look...

Parsing a date string gives [__NSDate length] unrecognized selector exception [duplicate]

objective-c,cocoa,cocoa-touch,nsdate,nsdateformatter

I would hazard a guess that item[@"enddata"] is already an NSDate object and not an NSString object. Verify with: NSLog(@"enddata is type %@", NSStringFromClass([item[@"enddata"] class])); Also don't use valueForKey:, but instead use objectForKey: or the newish access syntax I've used above....

iOS Date Formatter Returns Null

ios,objective-c,nsdate,nsdateformatter

Please use below code NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"]; NSString *dueDateString = [NSString stringWithFormat:@"2014-07-11T15:21:42.207+02:00"]; NSDate *dateFromApi = [dateFormatter dateFromString:dueDateString]; [dateFormatter setDateFormat:@"yyyy/MM/dd"]; NSString *finalDateString = [dateFormatter stringFromDate:dateFromApi]; NSLog(@"dueDateString %@", dueDateString); NSLog(@"dateFromApi %@", dateFromApi); NSLog(@"finalDateString%@",...

Get last time with period in iPhone 4 with NSDateFormatter

objective-c,nsdateformatter

I've just tested your provided code on my iOS 7.1 device, and it gave me "3:22", so problem is not device or OS version. Problem is the locale, do like this and it will work - (NSString *)getLastTimeMessageWithPeriod:(NSDate *)lastMessageDate { NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"h:mm a"]; [dateFormatter setLocale:[[NSLocale...

Not able to get NSDate from NSString

ios,objective-c,nsdate,nsdateformatter

The format string should be [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; Note the upper case .SSS. And for the locale, you should specify @"en_US_POSIX" as advised in Technical Note TN1480. This will ensure that you can successfully parse this RFC 3339/ISO 8601 formatted date, regardless of the user's localization settings....

NSDateFormatter doing wrong

swift,date,nsdateformatter

You just need to set the locale identifier to "pt_BR" // this returns the date format string "dd 'de' MMMM 'de' yyyy" but you still need to set your dateFormatter locale later on let br_DateFormat = NSDateFormatter.dateFormatFromTemplate("ddMMMMyyyy", options: 0, locale: NSLocale(localeIdentifier: "pt_BR")) let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = br_DateFormat dateFormatter.locale...

How to display time 'just now' '1 minute ago' '1 hour ago' respectively

ios,nsdate,nsdateformatter,nsdatecomponents

It looks like there are two questions here. The first one is about natural language date formatting, and I think you might find this relevant. The second question looks like you're having some confusion about time zones. NSDate is going to parse your date assuming GMT absent any indication of...

How do I create a new NSDate object that is in the past, e.g. 1 second earlier than another date?

ios,swift,nsdate,nsdateformatter,nsdatecomponents

You can achieve this by changing your second line from 1 to -1 like this var comp = NSDateComponents() comp.setValue(-1, forComponent: NSCalendarUnit. CalendarUnitSecond); let date: NSDate = NSDate() var expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(comp, toDate: date, options: NSCalendarOptions(0)) Hope it will help you....

NSDateFormatter for day and month name

ios,objective-c,nsdate,nsdateformatter

try this... //Getting date from string NSString *dateString = @"09-03-2015"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *date = [[NSDate alloc] init]; date = [dateFormatter dateFromString:dateString]; // converting into our required date format [dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy"]; NSString *reqDateString = [dateFormatter stringFromDate:date]; NSLog(@"date is %@", reqDateString); LOG:2015-03-09...

How to subtract time from NSDate with the help of NSDateFormatter?

ios,objective-c,nsdate,nsdateformatter

Try this you will get output: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm a"]; NSDate* selectedTime = [dateFormatter dateFromString:@"11:22 AM"]; NSDate* myTime = [selectedTime dateByAddingTimeInterval:-60*10];//10 minute deduction NSString* updateTime = [dateFormatter stringFromDate:myTime]; NSLog(@"New Time %@",updateTime); Output : New Time 11:12 AM Here you are subtracting 10 Minutes from selected...

Not getting correct date from NSDate

ios,nsdate,nsdateformatter

I got the solution for this itself only needed to replace [formatter setDateFormat:@"yyyy-mm-dd hh:mm:ss"]; with [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; and it worked....

Date format - Swift

ios,date,swift,nsdate,nsdateformatter

Should be like this if you want to skip all the 0: yyyy-MM-ddTHH:mm:ss The ISO standard format is yyyy-MM-dd'T'HH:mm:ss.SSS'Z'...

Create NSDate with String time from 1970

ios,objective-c,nsdate,nsdateformatter

I did not understand what you meant by "The time is formatted with 'time from 1970'" But here is how to convert a string formatted long containing the number of seconds since 1970 to a NSDate object: NSDate * date = [NSDate dateWithTimeIntervalSince1970:[longValueString longLongValue]]; Sometimes the time coming from the...

DateFormatter for format 'Fri, 19 Jun 2015 20:05:23 GMT'

ios,objective-c,nsdate,nsdateformatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; // "Fri, 19 Jun 2015 20:05:23 GMT" [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"]; ...

Display a negative time in Obj-C

ios,objective-c,nsdate,nsdateformatter,nstimeinterval

That should do the trick: NSDate *now = [NSDate date]; NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1]; NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2]; NSLog(@"totalTime1: %f", totalTime1); // => -60.00; NSLog(@"totalTime2: %f", totalTime2); // => 6023.00; //must always be this way int timeOffset = (int) (totalTime1 - totalTime2); NSLog(@"timeOffset: %d", timeOffset); //...

iOS NSDateFormatter ignoring timezone

ios,json,nsdateformatter

The problem is you are quoting the timezone format specifier. You want: [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; Note the lack of quote before the Z....

DateFormatter for format : 2014-12-08T14:11:32.636Z

objective-c,ios7,nsdateformatter

// Date formatter for your date string: 2014-12-08T14:11:32.636Z NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"]; // Date from your string NSDate *date = [dateFormatter dateFromString:@"2014-12-08T14:11:32.636Z"]; // T => Week Day // S => Fractional Second // Z => zone- Time Zone // Updating to your format for preffered style...

convert hard code nsstring to nsdate

objective-c,nsstring,nsdate,nsdateformatter

NSString * dateStr = @"13th May-2015"; NSArray *stringComponentArray = [dateStr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSMutableArray *workingArray = [NSMutableArray arrayWithArray:stringComponentArray]; NSMutableString *dateString = [[NSMutableString alloc]initWithString:[workingArray firstObject]]; [dateString replaceOccurrencesOfString:@"st" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[dateString length])]; [dateString replaceOccurrencesOfString:@"nd"...

Swift date formatting with NSDateFormatter

ios,swift,nsdateformatter

let dateShow = formatter.dateFromString(v["feedDate"].description) I would first check if v["feedDate"] isn't an optional, because in this case the v["feedDate"].description would probably contain string like Optional("2015-05-10T18:39:55+02:00") as opposed to 2015-05-10T18:39:55+02:00, which obviously won't get converted to NSDate... Edit: Under your original post you provided this information: this is what it's...

nsdate format to last digit in year, then day of year

nsdateformatter

Surely you could just use substringFromIndex on the string, something that should work for the next eight thousand years or so (a). In other words, something like: self.julian.text = [[julianFormatter stringFromDate:julianLabel] substringFromIndex:3]; (a) It'll work as per your specification until we reach the year 10,000 but keep in mind you'll...

NSDateFormatter detect 24-hour clock in OS X and iOS

swift,nsdate,nsdateformatter

The date template function has a neat trick. There is a template specifier j which will turn into an hour format depending if the locale uses 12 or 24 hour format. It'll turn into something like h a for 12 hour (en_US in this case) or HH for 24 hour...

NSDateFormatter returning nil with dateFromString

ios,objective-c,iphone,nsdate,nsdateformatter

NSString *[email protected]"1992-11-15"; NSDateFormatter *format = [[NSDateFormatter alloc] init]; //Set your input format [format setDateFormat:@"yyyy-MM-dd"]; //Parse date from input format NSDate *dateOfBirth = [format dateFromString:trimmedDOB]; //Set your output format [format setDateFormat:@"MM-dd-yyyy"]; //Output date in output format NSLog(@"DATE IS %@",[format stringFromDate:dateOfBirth]); ...

objective c - NSDateFormatter incorrect output [duplicate]

objective-c,xcode,nsdate,nsdateformatter

NSDateFormatter uses your current timezone (GMT+8) unless you explicitly set. You should set your formatter's timezone as UTC (in other words GMT) to format your date correctly. NSLog(@"Current timezone: %@", [NSTimeZone defaultTimeZone]); NSString *customDate = @"23-06-1993"; NSLog(@"Custom Date: %@", customDate); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MM-yyyy"]; [formatter setTimeZone:[NSTimeZone...

How to parse date with time in Objective C

ios,objective-c,ios8,nsdateformatter,ios7.1

setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ" (It's all in the spec if you bother to look.)...

iOS: dateformatter in swift return nil

ios,swift,nsdateformatter

Hate to break it :) Think it might be: "yyyy-MM-dd HH:mm:ss" not "yyyy-MM-dd HH:mm" ?...

How do I format JSON Date String with Swift?

json,swift,nsdateformatter

Use the following string format dateFor.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" ...

iOS Set Date format based on user locale

ios,nsdate,nsdateformatter

According to the documentation, the "Medium" style (that you are specifying) for English is shown as "Nov 23, 1937" so it shouldn't be very surprising that it was shown as "nov" instead of "11" in Italian. NSDateFormatterMediumStyle Specifies a medium style, typically with abbreviated text, such as “Nov 23, 1937”"...

Swift - extracting hours from NSDate, parsing string issue

ios,swift,nsdate,nsdateformatter

First of all, you have your formatter wrong... let formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'" var output = formatter.dateFromString("2000-01-01T00:00:00.000Z") After that, you can get the hour component with if let date = output { var hours = NSCalendar.currentCalendar().component(.HourCalendarUnit, fromDate: date) } ...

nsdateformatter — convert string to date

ios,objective-c,nsdateformatter

You need to take the fractional seconds into account (and not quote anything expect special characters.) This format string should work: "yyyy'-'MM'-'dd'T'HH':'mm':'S.SSSZZZZZ" ...

How to convert NSString to NSDate in IOS?

ios,objective-c,nsstring,nsdate,nsdateformatter

-(NSString *)geTimeFromString:(NSString *)string { NSString * dateString = [NSString stringWithFormat: @"%@",string]; NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm:ss"]; NSDate* myDate = [dateFormatter dateFromString:dateString]; [dateFormatter setDateFormat:@"hh:mm a"]; [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; NSString *stringFromTime = [dateFormatter stringFromDate:myDate]; return stringFromTime; } Try this...

NSDateComponentsFormatter's stringFromDate(_, toDate:) returns nil

swift,nsdate,nsdateformatter,nsdatecomponents,nsdatecomponentsformatter

From the headerdoc: /* Bitmask of units to include. Set to 0 to get the default behavior. Note that, especially if the maximum number of units is low, unit collapsing is on, or zero dropping is on, not all allowed units may actually be used for a given NSDateComponents. Default...

Getting back a date from a string

ios,swift,nsdateformatter

The problem there is that Y is for weekOfYear. You have to use "dd-MM-yyyy"

NSDate Issue While Creating it from NSString

ios,nsdate,nsdateformatter

Change with this one: [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; ...

Using dateFromString api of date formatter in a for loop giving huge memory issues

ios,objective-c,nsdate,nsdateformatter

Your @autoreleasepool is in the wrong place. You ned to be draining the pool at the end of each loop: for ( NSDictionary *dic in array) { @autoreleasepool { NSDate *date = [millisecondDateFormatter dateFromString:[dic objectForKey:@"key"]]; obj.nestedObj.startTime = date; date = nil; } } That said, I assume your real code...

NSDateFormatter returns nil sometimes

ios,nsdateformatter

NSDateFormatter follows the Unicode standard for date and time patterns. Use 'H' for the hour on a 24-hour clock: So in your code, the correct way should be: NSString *lastModifiedString = [metaData objectForKey:@"Last-Modified"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"]; NSDate *onlineModDate = [dateFormatter...

IOS Swift. Time since date

ios,swift,nsdateformatter

You need to use an NSCalendar to compute a difference in terms of months and days. Example: let calendar = NSCalendar.autoupdatingCurrentCalendar() calendar.timeZone = NSTimeZone.systemTimeZone() let dateFormatter = NSDateFormatter() dateFormatter.timeZone = calendar.timeZone dateFormatter.dateFormat = "yyyy-MM-dd" if let startDate = dateFormatter.dateFromString("2014-9-15") { let components = calendar.components( NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, fromDate: startDate, toDate:...

How do I change the displayed time format after NSTimer is initiated?

ios,objective-c,nstimer,nsdateformatter

The problem is that you have two different updateTimers -- self.updateTimer the class variable and updateTimer the local variable. You're invalidating the class variable, but initializing and running multiple local NSTimers with different locales during each call to startTimer. That's why you see this "flickering" -- it's because multiple NSTimers...

Have troubles with NSDateFormatter while parsing

objective-c,nsdateformatter

"CEST" is a "short specific non-location format" and the corresponding date field symbol is "z", not "v": [inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"]; In addition, you might have to set the date formatter locale explicitly to "en_GB" as explained in Nsdateformatter + Timezone issue....

ios8 Timezone parsing changed?

ios,nsdateformatter

Setting dateStyle or timeStyle will override any custom dateFormat you have set. Your date string does not conform in any way to NSDateFormatterShortStyle, which is why you are getting nil when trying to convert strings to dates. Furthermore, according to Unicode Technical Standard #35 revision 31, the timezone specifier of...