Fork me on GitHub

Parsing, validating, manipulating, and formatting dates easily in Objective-C

YLMoment is a library which provides an high abstraction level for parsing, validating, manipulating, and formatting the dates in Objective-C.

Its API is inspired by the well known moment.js library, however unlike its counterpart, its core is built upon the Foundation Framework components (NSDate, NSCalendar, etc.) to enable the interoperability with them.

This library is designed to facilitate the manipulation of times, dates, calendars, and durations in Objective-C by providing a single, easy, and unified approach to dealing with them.

Formatting Dates

          
// Display: October 27 2013, 10:49:48 AM
NSLog(@"%@", [[YLMoment now] format:@"MMMM dd yyyy, h:mm:ss a"]);

// Display: Oct 27 13
NSLog(@"%@", [[YLMoment now] format:@"MMM dd yy"]);

// Display: 2013 escaped 2013
NSLog(@"%@", [[YLMoment now] format:@"yyyy 'escaped' yyyy"]);

// Display: 2013-10-27T10:49:48+0100
NSLog(@"%@", [[YLMoment now] format]);
          
        

Time Zones

          
// Uses my current time zone: here the CET time (GMT+1)
YLMoment *now = [YLMoment now];
NSLog(@"%@", [now format]); // 2014-01-18T18:51:10+0100

// Change the time zone of the moment
now.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSLog(@"%@", [now format]); // 2014-01-18T17:51:10+0000

// Convenient way to get the UTC moment
YLMoment *utc = [YLMoment utc];
NSLog(@"%@", [utc format]); // 2014-01-18T17:51:10+0000
          
        

Relative Times

          
// Display: 2 years ago
NSLog(@"%@", [[YLMoment momentWithDateAsString:@"20111031"] fromNow]);

// Display: a year ago
YLMoment *m =
[YLMoment momentWithDateAsString:@"2012/06/20" format:@"yyyy/MM/dd"];
NSLog(@"%@", [m fromNow]);

// Display: 19 hours ago
NSLog(@"%@", [[[YLMoment now] startOf:@"day"] fromNow]);

// Display: in 5 hours
NSLog(@"%@", [[[YLMoment now] endOf:@"day"] fromNow]);

// Display: 11 minutes ago
NSLog(@"%@", [[[YLMoment now] startOf:@"hour"] fromNow]);
          
        

Languages

YLMoment currently supports Albanian, Chinese, Dutch, English, French, German, Italian, Japanese, Portuguese, Spanish and Vietnamese for relative times and all the native languages for the formatters.

          
// Display: il y a quelques secondes
YLMoment *french = [[YLMoment now] addAmountOfTime:-3 forUnitKey:@"s"];
[french setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_FR"]];
NSLog(@"%@", [french fromNow]);

// Display: hace unos segundos
YLMoment *spanish = [[YLMoment now] addAmountOfTime:-3 forUnitKey:@"s"];
[spanish setLocale:[NSLocale localeWithLocaleIdentifier:@"es_ES"]];
NSLog(@"%@", [spanish fromNow]);

// Display: in 11 months
YLMoment *reference = [YLMoment momentWithArray:@[@2013]];
YLMoment *english = [[YLMoment now] addAmountOfTime:-3 forUnitKey:@"s"];
[english setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
NSLog(@"%@", [english fromMoment:reference]);

// Display: em 11 meses
YLMoment *reference  = [YLMoment momentWithArray:@[@2013]];
YLMoment *portuguese = [[YLMoment now] addAmountOfTime:-3 forUnitKey:@"s"];
[portuguese setLocale:[NSLocale localeWithLocaleIdentifier:@"pt_BR"]];
NSLog(@"%@", [portuguese fromMoment:reference]);