NSDateFormatter “Month” in 3 letters instead of full word

后端 未结 7 1349
一生所求
一生所求 2021-02-01 03:00
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@\"dd-MM-YYYY HH:mm\"];        
    [formatter setTimeZone:[NSTimeZone syst         


        
相关标签:
7条回答
  • 2021-02-01 03:16

    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..
            ..
    
    0 讨论(0)
  • 2021-02-01 03:17

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

    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    0 讨论(0)
  • 2021-02-01 03:20

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

    0 讨论(0)
  • 2021-02-01 03:20

    In SWIFT

    let dateFormatter = NSDateFormatter()
    
    dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
    
    dateFormatter.stringFromDate(NSDate())
    
    0 讨论(0)
  • 2021-02-01 03:23

    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)

    0 讨论(0)
  • 2021-02-01 03:32

    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

    0 讨论(0)
提交回复
热议问题