Questions tagged [nsdateformatter]

Class that creates string representations of NSDate (and NSCalendarDate) objects, and convert textual representations of dates and times into NSDate objects.

There are many attributes you can get and set on a 10.4-style date formatter, including the locale, time zone, calendar, format string, and the various textual strings like the month names. You are encouraged, however, not to change individual settings. Instead you should accept the default settings established on initialization and specify the format using setDateStyle:, setTimeStyle:, and appropriate style constants.

References:

2932 questions
308
votes
25 answers

How do I convert a date/time string into a different date string?

How will I convert this datetime from the date? From this: 2016-02-29 12:24:26 to: Feb 29, 2016 So far, this is my code and it returns a nil value: let dateFormatter = NSDateFormatter() dateFormatter.dateFormat =…
Sydney Loteria
  • 10,171
  • 19
  • 59
  • 73
220
votes
18 answers

Convert string to date in Swift

How can I convert this string "2016-04-14T10:44:00+0000" into an NSDate and keep only the year, month, day, hour? The T in the middle of it really throws off what I am used to when working with dates.
asheyla
  • 3,175
  • 5
  • 18
  • 34
174
votes
4 answers

What is the best way to deal with the NSDateFormatter locale "feature"?

It seems that NSDateFormatter has a "feature" that bites you unexpectedly: If you do a simple "fixed" format operation such as: NSDateFormatter* fmt = [[NSDateFormatter alloc] init]; [fmt setDateFormat:@"yyyyMMddHHmmss"]; NSString* dateStr = [fmt…
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
132
votes
8 answers

Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds

I basically need to get current date and time separately, formatted as: 2009-04-26 11:06:54 The code below, from another question on the same topic, generates now: |2009-06-01 23:18:23 +0100| dateString: |Jun 01, 2009 23:18| parsed: …
bcsantos
  • 2,635
  • 5
  • 21
  • 22
125
votes
12 answers

How do I get an ISO 8601 date on iOS?

It's easy enough to get the ISO 8601 date string (for example, 2004-02-12T15:19:21+00:00) in PHP via date('c'), but how does one get it in Objective-C (iPhone)? Is there a similarly short way to do it? Here's the long way I found to do…
JohnK
  • 6,865
  • 8
  • 49
  • 75
100
votes
8 answers

iPhone OS: How do I create an NSDate for a specific date?

Seems like a simple thing but I can't seem to find a way to do it. It would be great to see a couple different methods.
nickthedude
  • 4,925
  • 10
  • 36
  • 51
97
votes
9 answers

How to get NSDate day, month and year in integer format?

I want to get the day, month and year components of NSDate in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer. How can I do this in iOS? I found the similar question but the method…
Yogi
  • 3,578
  • 3
  • 35
  • 56
94
votes
12 answers

Xcode Swift am/pm time to 24 hour format

I am trying to convert an am/pm format time to a 24 hour format time 6:35 PM to 18:35 I tried this piece of code on playground but it doesn't seem to work if I put the time alone let dateAsString = "02/12/15, 6:35 PM" let dateFormatter =…
user3772
  • 1,199
  • 2
  • 11
  • 15
91
votes
11 answers

iOS: How to get a proper Month name from a number?

I know that the NSDateformatter suite of functionality is a boon for mankind, but at the same time it is very confusing to me. I hope you can help me out. Somewhere in my code, there is an int representing a month. So: 1 would be January, 2…
Sjakelien
  • 2,255
  • 3
  • 25
  • 43
88
votes
7 answers

Convert NSDate to String in iOS Swift

I am trying to convert a NSDate to a String and then Change Format. But when I pass NSDate to String it is producing whitespace. let formatter = DateFormatter() let myString = (String(describing: date)) formatter.dateFormat = "yyyy-MM-dd…
Inder_iOS
  • 1,636
  • 1
  • 12
  • 20
86
votes
4 answers

Difference between 'YYYY' and 'yyyy' in NSDateFormatter

What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that 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.…
P.J
  • 6,547
  • 9
  • 44
  • 74
83
votes
11 answers

Get current NSDate in timestamp format

I have a basic method which gets the current time and sets it in a string. However, how can I get it to format the current date & time in a UNIX since-1970 timestamp format? Here is my code: NSDate *currentTime = [NSDate date]; NSDateFormatter…
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
83
votes
2 answers

What kind of sarcastic error is this iOS?

I have some code I use to sort calendar dates that looks like this: #if !(TARGET_IPHONE_SIMULATOR) NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0 …
random
  • 8,568
  • 12
  • 50
  • 85
68
votes
8 answers

Swift 3.0 : Convert server UTC time to local time and vice-versa

I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index =…
NiravS
  • 1,144
  • 1
  • 12
  • 24
63
votes
17 answers

Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat

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 15th NSDate *date = [NSDate…
Matt Andersen
  • 4,816
  • 3
  • 24
  • 19
1
2 3
99 100