MobilliumDateFormatter 1.3.0

MobilliumDateFormatter 1.3.0

aslanmehmetsalihMehmet Salih Aslan 维护。



MobilliumDateFormatter

Build Status Version License Platform

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install

要求

  • iOS 9.0+
  • Swift 5.0+

安装

MobilliumDateFormatter 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中。

pod 'MobilliumDateFormatter'

使用方法

MobilliumDateFormatter 包含这些格式:

/// 2-digit year
/// Example: 08
case yy

/// 4-digit year
/// Example: 2008
case yyyy

/// The quarter of the year. Use QQ if you want zero padding.
/// Example: 4
case Q

/// Quarter including "Q"
/// Example: Q4
case QQQ

/// Quarter spelled out
/// Example: 4th quarter
case QQQQ

/// The numeric month of the year. A single M will use '1' for January.
/// Example: 12
case M

/// The numeric month of the year. A double M will use '01' for January.
/// Example: 12
case MM

/// The shorthand name of the month
/// Example: Dec
case MMM

/// Full name of the month
/// Example: December
case MMMM

/// Narrow name of the month
/// Example: D
case MMMMM

/// The day of the month. A single d will use 1 for January 1st.
/// Example: 14
case d

/// The day of the month. A double d will use 01 for January 1st.
/// Example: 14
case dd

/// The day of week in the month
/// Example: 3rd Tuesday in December
case F

/// The day of week in the month
/// Example: Tues
case E

/// The full name of the day
/// Example: Tuesday
case EEEE

/// The narrow day of week
/// Example: T
case EEEEE

/// The 12-hour hour.
/// Example: 4
case h

/// The 12-hour hour padding with a zero if there is only 1 digit
/// Example: 04
case hh

/// The 24-hour hour.
/// Example: 16
case H

/// The 24-hour hour padding with a zero if there is only 1 digit.
/// Example: 16
case HH

/// AM / PM for 12-hour time formats
/// Example: PM
case a

/// The minute, with no padding for zeroes.
/// Example: 35
case m

/// The minute with zero padding.
/// Example: 35
case mm

/// The seconds, with no padding for zeroes.
/// Example: 8
case s

/// The seconds with zero padding.
/// Example: 08
case ss

或者创建自己的格式

import MobilliumDateFormatter

extension Date.Format {
    static let dateTime = Date.Format.custom(rawValue: "yyyy-MM-dd HH:mm:ss")
}

设置区域设置(可选)

MobilliumDateFormatter.locale = Locale(identifier: "us")

设置日历(可选),默认值:Calendar.current

MobilliumDateFormatter.calendar = Calendar.current

字符串转日期

let dateString = "2001-01-01 01:01:00"
let date = Date.from(dateString, format: .dateTime)

日期转字符串

let date = Date()
let dateString = date.to(.MMMM)

时间间隔转日期

let timeInterval = TimeInterval(exactly: 1549611277)!
let date = Date.from(timeInterval)

支持用于数学运算的日期组件类型

enum DateComponentType {
    case year
    case month
    case weekOfYear
    case day
    case hour
    case minute
    case second
    case millisecond
}

添加天数的示例(你可以这样添加另一个日期组件类型)

let date = Date()
let newDate = date.add(.day, count: 1)
// or
let newDate = now.addDay(-1)

许可证

MobilliumDateFormatter 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。