A lightweight Swift date library for parsing, validating, manipulating, and formatting dates based on moment.js.

Overview

moment.swift

A lightweight Swift date library for parsing, validating, manipulating, and formatting dates based on moment.js.

Under the hood

The goal of this project is to help developers leverage the power of moment.js in their Swift applications. Moment.swift uses the JavaScriptCore Framework to run moment.js from within Swift. Under the hood, moment.swift is simply a wrapper around the moment.js library. By sharing the same code base, moment.swift can benefit from years of testing and bug fixes and can be easily updated every time moment.js is updated.

Usage

Drag and drop moment.swift and moment-with-locales.min.js into your Xcode project and you're good to go.

Supported version 2.19.1

Multiple locale support

Out of the box, moment.swift supports a total of 239 locales. See the moment.js website for a list of all supported locales.

Documentation

The official moment.js documentation can be used with moment.swift. Just make sure to take into account the few syntax variations between the JavaScript and Swift versions.

Syntax differences

The syntax of moment.swift is almost identical to its JavaScript counterpart, thanks to the similarities between the two languages. Almost all methods have been implemented without affecting the syntax. Below is a list of syntax differences between the JavaScript and Swift versions. The official documentation of moment.js can be used by taking into account the following variations:

Objects

The main difference is in the way objects are declared in Swift, { hour:15, minute:10 } becomes [ "hour":15, "minute":10 ]:

let today = moment([ "hour":15, "minute":10 ])

null

The JavaScript null data type becomes NSNull():

let isBetween = moment("2016-10-30").isBetween("2016-10-30", "2016-12-30", NSNull(), "()")

Date() objects

Moment.swift supports the native Swift Date() object. When passing a Date() to moment.swift, it is automatically converted to its JavaScript counterpart and vice versa:

let today = moment(Date())
let dateObj = today.toDate()

Functions

To use a Swift closure with moment.swift, declare it with the @convention(block) attribute:

let closure: @convention(block) (String) -> String = { now in
    // Do something
}

moment().calendar(NSNull(), [
    "sameDay": closure
])

Built with

  • moment.js - Parse, validate, manipulate, and display dates in javascript.
  • Swift - The powerful programming language that is also easy to learn.
  • JavaScriptCore Framework - Evaluate JavaScript programs from within your Swift app.

Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

Update the README.md with details of changes to the library.

Update the tests by demonstrating the changes to the library.

Build the project and test all the features before submitting your pull request.

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details.

You might also like...
An original project making use of custom UITableViewCells, date formatting, json parsing, and handling user location.

SunTimes An original project making use of custom UITableViewCells, date formatting, json parsing, date and time formatting based on the json data, an

TPInAppReceipt is a lightweight, pure-Swift library for reading and validating Apple In App Purchase Receipt locally.
TPInAppReceipt is a lightweight, pure-Swift library for reading and validating Apple In App Purchase Receipt locally.

TPInAppReceipt is a lightweight, pure-Swift library for reading and validating Apple In App Purchase Receipt locally. Features Read all

DateHelper - A high performant Swift Date Extension for creating, converting, comparing, or modifying dates.
DateHelper - A high performant Swift Date Extension for creating, converting, comparing, or modifying dates.

DateHelper A high performant Swift Date Extension for creating, converting, comparing, or modifying dates. Capabilities Creating a Date from a String

A framework to provide logic designed to prompt users at the ideal moment for a review of your app/software

ReviewKit ReviewKit is a Swift package/framework that provides logic designed to prompt users at the ideal moment for a review of your app. At a basic

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Utility functions for validating IBOutlet and IBAction connections
Utility functions for validating IBOutlet and IBAction connections

Outlets Utility functions for validating IBOutlet and IBAction connections. About Outlets provides a set of functions which validate that IBOutlets ar

Input Validation Done Right. A Swift DSL for Validating User Input using Allow/Deny Rules
Input Validation Done Right. A Swift DSL for Validating User Input using Allow/Deny Rules

Valid Input Validation Done Right. Have you ever struggled with a website with strange password requirements. Especially those crazy weird ones where

A library that expresses a github contribution calendar through an array of dates. Supports iOS and macOS.
A library that expresses a github contribution calendar through an array of dates. Supports iOS and macOS.

A library that expresses a github contribution calendar through an array of dates. Supports iOS and macOS.

CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS
CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS

CoreML-Face-Parsing The simple sample how to use face-parsing CoreML model in iO

Scan the MRZ code of a passport and extract the firstname, lastname, passport number, nationality, date of birth, expiration date and personal numer.
Scan the MRZ code of a passport and extract the firstname, lastname, passport number, nationality, date of birth, expiration date and personal numer.

PassportScanner Works with 2 and 3 line identity documents. What is this With PassportScanner you can use your camera to scan the MRZ code of a passpo

A "time ago", "time since", "relative date", or "fuzzy date" category for NSDate and iOS, Objective-C, Cocoa Touch, iPhone, iPad

Migration 2014.04.12 NSDate+TimeAgo has merged with DateTools. DateTools is the parent project and Matthew York is the project head. This project is n

Date Formatter Pool - is a small utility that creates and stores your Date Formatter for simpler reuse
Date Formatter Pool - is a small utility that creates and stores your Date Formatter for simpler reuse

Date Formatter Pool Date Formatter Pool - is a small utility that creates and stores your Date Formatter for simpler reuse Installation is available i

RCalendarPicker A date picker control, Calendar calendar control, select control, calendar, date selection, the clock selection control.
RCalendarPicker A date picker control, Calendar calendar control, select control, calendar, date selection, the clock selection control.

RCalendarPicker RCalendarPicker Calendar calendar control, select control, calendar, date selection, the clock selection control. 日历控件 ,日历选择控件,日历,日期选择

A library for formatting strings on iOS and macOS

Sprinter Introduction What? Why? How? Usage Installation Integration Localization Thread Safety Advanced Usage Introduction What? Sprinter is a librar

A Cocoa NSFormatter subclass to convert dates to and from ISO-8601-formatted strings. Supports calendar, week, and ordinal formats.
A Cocoa NSFormatter subclass to convert dates to and from ISO-8601-formatted strings. Supports calendar, week, and ordinal formats.

ISO 8601: The only date format worth using Obligatory relevant xkcd: How to use this code in your program Add the source files to your project. Parsin

SwiftDate 🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
SwiftDate 🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.

Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift. What's This? SwiftDate is the definitive toolchain to ma

Dates and times made easy in iOS
Dates and times made easy in iOS

DateTools DateTools was written to streamline date and time handling in iOS. Classes and concepts from other languages served as an inspiration for Da

Punctual - Swift dates, more fun. Heavily inspired by ObjectiveSugar

Punctual Swift dates, more fun. Heavily inspired by ObjectiveSugar Installation Punctual is available through the Swift Package Manager! Just add this

🕐 Format your dates/times as emojis.
🕐 Format your dates/times as emojis.

EmojiTimeFormatter Format your dates/times as emojis 🕟 🕑 🕗 Description You can build easy to understand user interfaces with EmojiTimeFormatter or

Owner
Olabisi Oduola
A full-stack developer building and scaling systems - sometimes of a distributed nature • Frontend with the code, Backend with the strokes •
Olabisi Oduola
null 13 Oct 28, 2022
Repository to store the projects made during the 100 Days of Swift challenge by Paul Hudson of Hacking with Swift.

100DaysOfSwift ?? Repository to store the projects made during the 100 Days of Swift challenge by Paul Hudson of Hacking with Swift. Days 1-12: Introd

Rayana Prata 5 Apr 26, 2022
This is the ninth project from Hacking With Swift 100 days of Swift course.

Petitions Viewer This is the ninth project from Hacking With Swift 100 days of S

Bruno Guirra 2 Feb 23, 2022
Xcode projects and sources created during HWS-Live workshop hosted by @twostraws

Hacking with Swift Live 2021 My Xcode projects and sources created during Hacking With Swift Live online-workshops hosted by @twostraws. Day 1 Various

Philipp 6 May 4, 2022
SwiftUIDay49To61 - Includes CoreData, Networking learning and my custom MVVM CoreData implementation from 100 days of SwiftUI

Includes CoreData, Networking learning and my custom MVVM CoreData implementation from 100 days of SwiftUI by "Hacking with Swift"

Vong Nyuk Soon 3 Feb 11, 2022
A guide on setting up Xcode with all the essential Applications, Tools, and Frameworks to make your development experience with Xcode great!

A guide on setting up Xcode with all the essential Applications, Tools, and Frameworks to make your development experience with Xcode great!

Michael Royal 24 Jan 4, 2023
Learning Swift using Docker containers as development environment

100-days-of-swift Small setup to help me learn Swift. I don't own a mac so this is what I came up with ?? .

Diogo Cavaleiro 1 Feb 14, 2022
Programming in Swift 5.5 - course at Sofia University St. Kliment Ohridski

Програмиране със Swift 5.5 Това е git репозиторито, където ще качваме материалите за курса "Програмиране със Swift" към ФМИ през летния семестър на уч

null 6 Oct 19, 2022
A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.

PhoneNumberKit Swift 5.3 framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber. Features F

Roy Marmelstein 4.7k Dec 29, 2022
A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.

PhoneNumberKit Swift 5.3 framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber. Features F

Roy Marmelstein 4.7k Jan 8, 2023