GithubHelp home page GithubHelp logo

yannickl / ylmoment Goto Github PK

View Code? Open in Web Editor NEW
709.0 21.0 68.0 516 KB

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

Home Page: http://yannickl.github.io/YLMoment

License: MIT License

Ruby 0.89% Objective-C 99.01% Shell 0.10%

ylmoment's Introduction

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(@"%@", [albanian fromNow]); // disa sekonda me parë

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

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

Supported languages

Here the list of supported languages:

  • Albanian
  • Chinese
  • Dutch
  • English
  • French
  • German
  • Italian
  • Japanese
  • Portuguese
  • Spanish
  • Vietnamese

All contribution is welcomed! ;)

Installation

The recommended approach to use YLMoment in your project is using the CocoaPods package manager, as it provides flexible dependency management and dead simple installation.

CocoaPods

Install CocoaPods if not already available:

$ [sudo] gem install cocoapods
$ pod setup

Go to the directory of your Xcode project, and Create and Edit your Podfile and add YLMoment:

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '5.0'
# Or platform :osx, '10.8'
pod 'YLMoment', '~> 0.9.1'

Install into your project:

$ pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file)

$ open MyProject.xcworkspace

Manually

Download the project and copy the YLMoment folder into your project and then simply #import "YLMoment.h" in the file(s) you would like to use it in.

Contribution

Contributions are welcomed and encouraged .

Contact

Yannick Loriot

License (MIT)

Copyright (c) 2013-present - Yannick Loriot

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ylmoment's People

Contributors

alexshepard avatar applezqp avatar bitdeli-chef avatar douglasmedeiros avatar edelabar avatar jwyterlin avatar mcdooda avatar pieterclaerhout avatar rcgary avatar slizeray avatar strictlyd avatar vjyanand avatar yannickl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ylmoment's Issues

Localization problem time string change locale without config.

Before update to 0.3.0, I think my locale default with 'en' but after update to 0.3.0 my locale was automatic change to 'de' (I notice by the string it was produced). I don't know why. Because I never touch the localization setting in my app before.

How can I have my translations?

I'm currently using YLMoment with cocoapods, but I want to have some changes in the strings file. I could fork the repo and make the changes on the strings files, but It should be possible to have a different bundle with costumized strings files

Locale Seeming Can't Be Changed Multiple Times

I'm writing a RubyMotion wrapper for this Cocoapod and it seems that the only way to set the locale is to set it on the class-level proxy member. I can do this successfully, but subsequent attempts to change that locale have no effect.

Questions:

  • Is accessing the proxy member the right way to go?
  • Why would the second attempt fail?

Consider the scenario of tests where the I test first en_US, then fr_FR, etc.

Thanks

How can I use dateStyle and timeStyle properties

moment.dateStyle = NSDateFormetterStyle.MediumStyle
moment.dateStyle = NSDateFormatterStyle.NoStyle     
moment.format(nil)

This code works great. I'm not sure calling format with nil is the right way?

Using YLMoment as a CocoaPod replaces the app's Localization.strings file with its own

A couple of hours of frustration as to why my localised strings were not making it into the app had me eventually pull the app bundle apart to find that the Localizable.strings files were being clobbered by YLMoment's ones. HockeySDK also uses localisation but its files are named HockeySDK.strings and are contained within a separate bundle. I think the fix for this would be to move the NSLocalizedString calls to point to a YLMoment bundle file instead.

Updating Podspec to 0.5.2 and push to CocoaPods

You've made some improvements that solve problems like this:

- NOTE  | [xcodebuild]  YLMoment/YLMoment/YLMoment.m:198:47: warning: comparison of constant -1 with expression of type 'NSDateFormatterStyle' (aka 'enum NSDateFormatterStyle') is always true [-Wtautological-constant-out-of-range-compare]
- NOTE  | [xcodebuild]  YLMoment/YLMoment/YLMoment.m:199:47: warning: comparison of constant -1 with expression of type 'NSDateFormatterStyle' (aka 'enum NSDateFormatterStyle') is always true [-Wtautological-constant-out-of-range-compare]
- NOTE  | [xcodebuild]  YLMoment/YLMoment/YLMoment.m:361:24: warning: 'week' is deprecated: first deprecated in iOS 7.0 - Use weekOfMonth or weekOfYear, depending on which you mean [-Wdeprecated-declarations]
- NOTE  | [xcodebuild]  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSCalendar.h:433:1: note: 'week' has been explicitly marked deprecated here
- NOTE  | [xcodebuild]  YLMoment/YLMoment/YLMoment.m:361:24: warning: 'setWeek:' is deprecated: first deprecated in iOS 7.0 - Use setWeekOfMonth: or setWeekOfYear:, depending on which you mean [-Wdeprecated-declarations]

Are you able to publish a new version to CocoaPods?

The NS prefix should only be used by Apple.

May not seem like a big issue, but it is. Per Apple docs:

You’ll have noticed that Cocoa and Cocoa Touch class names typically start either with NS or UI. Two-letter prefixes like these are reserved by Apple for use in framework classes.

[..]

Your own classes should use three letter prefixes.

dd instead of DD?

Hey, just curious why you're not following the moment.js syntax for day of month? Moment.js is DD, whereas YLMoment is dd.

`isBeforeMoment` / `isAfterMoment` / `isBetweenMoments` are broken

Example :

YLMoment *m1 = [YLMoment momentWithDateAsString:@"01/01/2016 01:00:00" format:@"dd/MM/yyyy hh:mm:ss"];
YLMoment *m2 = [YLMoment momentWithDateAsString:@"01/01/2017 00:00:00" format:@"dd/MM/yyyy hh:mm:ss"];

[m1 isBeforeMoment:m2] // Should be true but it's false because the hour component from second moment is smaller than the one from first moment

isAfterMoment and isBetweenMoments are broken too because they use isBeforeMoment to work

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.