Swift: NSDate formatting with strftime & localtime

16,612

Solution 1

    let maxSize: UInt = 11
    var buffer: CChar[] = CChar[](count: Int(maxSize), repeatedValue: 0)
    var time: time_t = Int(NSDate().timeIntervalSince1970)
    var length = strftime(&buffer, maxSize, "%-l:%M\u2008%p", localtime(&time))
    var dateString = NSString(bytes: buffer, length: Int(length), encoding: NSUTF8StringEncoding)
    NSLog("dateString: %@", dateString) // dateString: 11:56 PM

Solution 2

As the commentators @BryanChen and @JasonCoco said, use NSDateFormatter.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd 'at' h:mm a" // superset of OP's format
let str = dateFormatter.stringFromDate(NSDate())

A full description of the format strings is available in "Data Formatting Guide".

Solution 3

Here is another example that uses NSDateFormatterStyle:

private func FormatDate(date:NSDate) -> String {
  let dateFormatter = NSDateFormatter()
  dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
  return dateFormatter.stringFromDate(date)
}

The output is formatted as, "January 1, 1990".

If you want to read more about the formatter and the different available styles, checkout, NSFormatter under NSDateFormatter section.

Share:
16,612

Related videos on Youtube

ma11hew28
Author by

ma11hew28

Updated on September 15, 2022

Comments

  • ma11hew28
    ma11hew28 over 1 year

    How do I convert the following Objective-C code into Swift code?

    #define MAX_SIZE 11
    char buffer[MAX_SIZE];
    time_t time = [[NSDate date] timeIntervalSince1970];
    strftime(buffer, MAX_SIZE, "%-l:%M\u2008%p", localtime(&time));
    NSString *dateString = [NSString stringWithUTF8String:buffer];
    NSLog(@"dateString: %@", dateString); // dateString: 11:56 PM
    

    I'm formatting a date.

    • Bryan Chen
      Bryan Chen almost 10 years
      use NSDateFormatter
    • Jason Coco
      Jason Coco almost 10 years
      I would just use an NSDateFormatter instead of strftime(3)
    • Gargoyle
      Gargoyle over 8 years
      Step telling people to do something that doesn't answer the question. There are multiple valid reasons to not use NSDateFormatter, and you not answering the OP doesn't help either him or anyone else looking for this exact same thing. So tired of finding "use NSDateFormatter" when I try to find strftime. Sigh.
  • zaph
    zaph almost 10 years
    DO NOT DO THIS! One point of Swift is to get away from "C" and explicit pointers toward a more safe language. Here we not only have "C" syntax but the possibility to over-run buffer with a future code change. This is not even acceptable "C" code from a safety perspective.
  • zaph
    zaph almost 10 years
    Date formatting symbols: ICU Formatting Dates and Times
  • CouchDeveloper
    CouchDeveloper over 9 years
    @joan @zaph To be honest, with some modifications this would be a viable approach and a valid alternative for a more performance critical scenario. NSDateFormatter is notoriously slow and when performance is critical we need to manage a cached formatter - or we may be happy with this approach.
  • Mathijs Segers
    Mathijs Segers almost 9 years
    I'm sorry but how is this using strformat? The OP Clearly asks for this, not for this solution.
  • Mathijs Segers
    Mathijs Segers almost 9 years
    I don't get why other things are upvoted and this is downvoted, this is the only real answer to OP's question, also his own answer of course. But if you want to use a strformat string, you need to use something like this the NSformatter won't support it.
  • Grimxn
    Grimxn almost 9 years
    Read @zaph's comments below, at the time of this question & answer. strformat is inherently unsafe, and this code effects the same output as the OP requested using safe Swift constructs.
  • Jordan Smith
    Jordan Smith almost 8 years
    @Grimxn @Unome this is not the correct answer. NSDateFormatter can be extremely performance degrading when parsing many dates, and is likely why the original code does not use it. This is not the answer to the question.
  • Unome
    Unome over 7 years
    @Jordan, commenting negatively on peoples answers to promote your own is BM. This code is very simple and useful FOR scenarios that you don't need to parse many dates. FOR scenarios that require parsing lots of dates, I agree that your lower level solution is better AT the cost of readability and complexity. Neither answer is write or wrong. And the OP selected hasn't selected either as the correct answer. I will remove my "Accept this answer" comment since multiple answers are useful in this post.
  • Jordan Smith
    Jordan Smith over 7 years
    @Unome not trying to promote my own answer sorry, I don't care how many up votes my answer gets. The comment was meant to align with the above comment by Mathjis Segers: the question asked how to convert code, not how to do something a different way. It's like asking how to say something in Spanish, and getting told that most people speak English anyway - no Spanish answer given.