NSDateFormatter "Month" in 3 letters instead of full word
Asked Answered
Q

7

36
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-YYYY HH:mm"];        
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];

If i choose MM i get the month in number: 09-05-2012 15:33 If i choose MMMM i get the month in word: 09-May-2012 15:33

What i wanted, was the month in 3 letters abbreviation.
Eg: January would be Jan

In this case May is correct because it only has 3 letters.

Quip answered 9/5, 2012 at 14:56 Comment(1)
Use MMM for short month nameBaghdad
Q
57

Have you tried: [formatter setDateFormat:@"dd-MMM-YYYY HH:mm"]; and then [formatter stringFromDate:someNSDate];

Quatrefoil answered 9/5, 2012 at 15:0 Comment(4)
M gives me number MMM gives me full wordQuip
Maybe you are right, i´m trying with May. Should use a different month, but i´m on the simulator...Quip
Hahaha, "May" is the only non-conclusive month to test this on.Chesney
This post helped me: #35969683Monologue
R
32

IF you get the two things in the wrong order, you don't get the expected output.

[formatter setDateFormat:@"MMM"];
[formatter setDateStyle:NSDateFormatterMediumStyle];

does NOT give the same results as:

[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"MMM"];

(The second of these two snippets is now happily handing out Sep Oct etc in my app)

Ruination answered 21/8, 2012 at 16:47 Comment(1)
dateStyle and dateFormat are different things. One overrides the other, NSDateFormatterMediumStyle uses the system wide format for short dates, while setting the dateFormat to MMM specifies that only the shot month value should be used.Spallation
P
9

In SWIFT

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "dd MMM yyyy HH:mm"

dateFormatter.stringFromDate(NSDate())
Peggypegma answered 9/2, 2016 at 10:7 Comment(0)
B
7

This is a stripped down solution that works with Swift 4 in producing a 3 letter month output:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY, HH:mm:ss"

And to verify:

print(dateFormatter.string(from: Date())) // 20 Mar 2018, 23:41:40

As previously noted, MMM is the correct syntax for getting the desired month output.

Bonnes answered 21/3, 2018 at 6:43 Comment(0)
H
3

for swift 4 go with:

if let dateFormatter = DateFormatter(){
    dateFormatter.dateStyle = .medium
    // eventually:
    //let locale = Locale(identifier: ...                       
    //dateFormatter!.locale = locale
    dateFormatter.dateStyle = .medium
    dateFormatter!.dateFormat = "dd MMM YYYY, HH:mm:ss"
 } else..
        ..
Hi answered 13/3, 2018 at 15:35 Comment(0)
M
2

Actually you can set your dateStyle to medium (ie 'Jan', 'Feb' etc) like this:

[formatter setDateStyle:NSDateFormatterMediumStyle];
Madeira answered 9/5, 2012 at 15:3 Comment(0)
H
1

For those who do not get a full month strong with "MMM", you can get it with

dateFormatter.dateFormat = "dd MMMM yyyy"

Tested on Swift 4.2

Householder answered 5/10, 2019 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.