GithubHelp home page GithubHelp logo

ramonski / nsdate-prettydate Goto Github PK

View Code? Open in Web Editor NEW
33.0 6.0 3.0 1.09 MB

Display relative date strings from NSDate objects similar to the times in GitHub commits

License: MIT License

Objective-C 100.00%

nsdate-prettydate's Introduction

NSDate+PrettyDate

An Objective-C category to calculate relative time spans between an NSDate object and the current date.

The result is simmilar to the commit times of GitHub, e.g. 2 hours ago.

Installation

Just drop the files NSDate+PrettyDate.m and NSDate+PrettyDate.h in your project. If you like, you can also put the german translation folder de.lproj in your project.

Example

ViewController.m:

#import "NSDate+PrettyDate.h"

- (void) viewDidLoad
{
    [super viewDidLoad];

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:-3615];

    self.label.text = [date prettyDate]; // results "1 hour ago"
    //self.label.text = [date prettyDate2]; // results "1 hour ago and 15 seconds ago"
}

Localization

To localize the resulting strings, do the following steps:

  1. Add a file Localizable.strings to your project. (File>New>File... Resources>Strings File)

  2. Add the desired language to your project (Click on Project>Localizations> +)

  3. Click on the Localizable.strings file, click Localize in the File inspector.

  4. Add the following contents:

    // PrettyDate
    "future date"    = "zukünftiges Datum";
    "just now"       = "in diesem Moment";
    "a minute ago"   = "vor einer Minute";
    "last hour"      = "letzte Stunde";
    "yesterday"      = "gestern";
    "last week"      = "letzte Woche";
    "last month"     = "letzten Monat";
    "last year"      = "letztes Jahr";
    
    "%d seconds ago" = "vor %d Sekunden";
    "%d minutes ago" = "vor %d Minuten";
    "%d hours ago"   = "vor %d Stunden";
    "%d days ago"    = "Vor %d Tagen";
    "%d weeks ago"   = "vor %d Wochen";
    "%d months ago"  = "vor %d Monaten";
    "%d years ago"   = "vor %d Jahren";
    
    // PrettyDate2
    "%d seconds ago ..."            = "vor %d Sekunden ...";
    "a few seconds ago"             = "vor ein paar Sekunden";
    
    "%d minutes and %d seconds ago" = "vor %d Minuten und %d Sekunden";
    "one minute and %d seconds ago" = "vor einer Minute und %d Sekunden";
    
    "%d hours and %d minutes ago"   = "vor %d Stunden und %d Minuten";
    "one hour and %d minutes ago"   = "vor einer Stunde und %d Minuten";
    
    "%d days and %d hours ago"      = "vor %d Tagen und %d Stunden";
    "one day and %d hours ago"      = "vor einem Tag und %d Stunden";
    
    "%d weeks and %d days ago"      = "vor %d Wochen und %d Tagen";
    "one week and %d days ago"      = "vor einer Woche und %d Tagen";
    
    "%d months and %d days ago"     = "vor %d Monaten und %d Tagen";
    "one month and %d days ago"     = "vor einem Monat und %d Tagen";
    
    "%d years and %d months ago"    = "vor %d Jahren und %d Monaten";
    "one year and %d months ago"    = "vor einem Jahr und %d Monaten";
    

Note

The german translation is already included in the NSDate+PrettyDate Project.

Demo Style 1

demo_images/style1_yesterday.jpg

demo_images/style1_en.jpg

demo_images/style1_ger.jpg

Demo Style 2

demo_images/style2_yesterday.jpg

demo_images/style2_en.jpg

demo_images/style2_ger.jpg

License

MIT. do what you want.

Changelog

0.1 - 2013-05-24

  • #1: Input: some date in future, output: "just now"
    Handle future dates correctly. Thanks to nrgbodya
  • fixed unittests and documentation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.