Git Product home page Git Product logo

iso8601's Introduction

ISO8601

Version Build Status Coverage Status Carthage compatible CocoaPods compatible

Fast ISO8601 date parser and writer for iOS & Mac.

Installation

Carthage is the recommended way to install ISO8601. Add the following to your Cartfile:

github "soffes/ISO8601"

You can also install with CocoaPods:

pod 'ISO8601'

For manual installation, I recommend adding the project as a subproject to your project or workspace and adding the framework as a target dependency.

Usage

First, import the appropriate header:

@import ISO8601; // Use #import <ISO8601/ISO8601.h> if you're using CocoaPods

This library uses NSDateComponents for reading and writing. Here's an example:

// Reading
NSDateComponents *dateComponents = [ISO8601Serialization dateComponentsForString:@"1999-05-19T23:55:21+09:00"];

// Writing
NSString *ISO8601String = [ISO8601Serialization stringForDateComponents:dateComponents];

There is an NSDate category for convenient conversion:

// Reading
NSDate *date = [NSDate dateWithISO8601String:@"1999-05-19T23:55:21+09:00"];

// Writing
NSString *ISO8601String = [date ISO8601String];

If you require more control over conversion or need to know the input time zone, you can use the advanced methods the category provides:

// Reading
NSTimeZone *timeZone;
NSDate *date = [NSDate dateWithISO8601String:@"1999-05-19T23:55:21+09:00" timeZone:&timeZone usingCalendar:calendarOrNil];

// Writing
NSString *ISO8601String = [date ISO8601StringWithTimeZone:timeZoneOrNil usingCalendar:calendarOrNil];

Notes

NSDateComponents is the core data structure because NSDate doesn't perserve time zone information well.

It's worth noting that a value in the NSDateComponents will be nil if it is not in the input string. For example, 1999-05-19T23:55:21 will have a nil time zone, but 1999-05-19T23:55:21+00:00 and 1999-05-19T23:55:21Z will have a UTC time zone.

The +[NSDate dateWithISO8601String:] category will always return a UTC date. If you want a date in another time zone, you should use +[NSDate ISO8601StringWithTimeZone:usingCalendar:] (you may pass nil for the calendar parameter to use the current calendar).

Enjoy.

iso8601's People

Contributors

calebd avatar florianbuerger avatar nap-sam-dean avatar neonichu avatar soffes avatar

Watchers

 avatar  avatar

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.