Skip to content

Parsing, validating, manipulating, and formatting dates easily in Objective-C (API inspired by moment.js)

License

Notifications You must be signed in to change notification settings

yannickl/YLMoment

Repository files navigation

YLMoment

License Supported Platforms Version Build Status codecov

🕐 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.

Getting StartedUsageInstallationContributionContactLicense

Getting Started

If you want have a quick overview of the project take a look to this blog post.

Usage

Formatting Dates

YLMoment *moment = [YLMoment now];

NSLog(@"%@", [moment format:@"MMMM dd yyyy, h:mm:ss a"]); // October 27 2013, 10:49:48 AM
NSLog(@"%@", [moment format:@"MMM dd yy"]);               // Oct 27 13
NSLog(@"%@", [moment format:@"yyyy 'escaped' yyyy"]);     // 2013 escaped 2013
NSLog(@"%@", [moment format]);                            // 2013-10-27T10:49:48+0100

Relative Times

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

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

NSLog(@"%@", [[[YLMoment now] startOf:@"day"] fromNow]);  // 19 hours ago
NSLog(@"%@", [[[YLMoment now] endOf:@"day"] fromNow]);    // in 5 hours
NSLog(@"%@", [[[YLMoment now] startOf:@"hour"] fromNow]); // 11 minutes ago

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 use get the UTC time
YLMoment *utc = [YLMoment utc];
NSLog(@"%@", [utc format]); // 2014-01-18T17:51:10+0000

Languages

YLMoment supports a lot of languages (see the list below) and is easy to use.

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

YLMoment *albanian = [[YLMoment now] addAmountOfTime:-3 forUnitKey:@"s"];
[albanian setLocale:[NSLocale localeWithLocaleIdentifier:@"sq_AL"]];
NSLog(@"%@</