SwiftDateに
Swiftdate
SwiftDate
Githubのオープンソースであり、Swift言語のNSDateパッケージライブラリを使用します。出力と比較して、作成日などのSwift日付で簡単に処理できます。
特性
- 日付計算の数学演算子(myDate + 2.week + 1.hourなど)をサポートします
- 比較演算子(、==、=など)をサポートします
- さまざまな部分のクイックアクセス/変更日(月のアクセスまたは変更日など)
- 共通の出力形式またはカスタム形式の出力を提供する
- さまざまなメソッドを提供します.toString
- 昨日、明日などを取得する簡単な方法を提供します。
頼る
- iOS 8.0以降/ Mac OS X 10.10以降
- Xcode 6.4
- スイフト1.2
Swift2.0バージョン、段落内のテキストの最後にあるGithubアドレスをサポートします
使用する
CocoaPodsのインストールSwiftDate
cocoapodsをインストールしておらず、次のコマンドでインストールできる場合は、CocoaPodsのインストールSwitDateが必要です。
var d1 = Date(year : 2014, month : 5, day : 15) var d2 = Date(string : '2014-05-15') |
次に、PodfileでXcodeプロジェクトをファイルし、以下を追加します。
d1 = d1 + 1 if (d2 > d1) { ... } |
最後に、次のコマンドを実行してインストールします
var d = Date(string : '2014-04-30') d.addMonths(1) // returns '2014-05-30' d.addMonths(1, rollDay : RollDay.ThirtyOne) // returns '2014-05-31' d + '1M' // returns '2014-05-30' |
作成日
- 文字列を解析して作成
var cal = USNYSECalendar() var d1 = Date(string : '2014-07-03') var nbd = cal.nextBizDay(d1) // returns '2014-07-07' - skipped over 4th of July! |
- 指定された日付のさまざまな部分によって作成されました
var dc = Actual360() dc.dayCountFraction(Date(string : '2014-01-31'), date2: Date(string : '2014-02-28')) |
- Stringクラスのメソッドによって作成されたToDate
$ gem install cocoapods |
- 静的メソッドNSDateによって作成されました
source 'https://github.com/CocoaPods/Specs.git' |
日付や日付などの情報へのアクセス
次のプロパティのNSDateを介して取得できます
$ pod install |
変更日
let date_custom = NSDate.date(fromString: '2015-07-26', format: DateFormat.Custom('YYYY-MM-DD')) |
運用日
let date_from_components = NSDate.date(refDate: nil, year: 2014, month: 01, day: nil, hour: nil, minute: nil, second: nil, tz: 'UTC') |
タイムゾーンコンバータ
let date = '2015-07-26'.toDate(formatString: 'YYYY-MM-DD') |
日付比較
数学演算子で比較できます
let todayDate = NSDate.today() |
NSDateの次のメソッドを比較することもできます
.year |
var date = NSDate() |
NSDateを文字列に
let date = NSDate() |
変換プロセスで指定することもできますNSDateFormatterStyle
let date = NSDate() // local time zone |
次の方法で特定の文字列に変換することもできます
let date1 = NSDate.date(fromString: '2015-07-26', format: DateFormat.Custom('YYYY-MM-DD')) |
最後に、「2時間前」の出力など、時間に関する形式を出力することもできます。
let isInRange : Bool = date1.isInTimeRange('11:00','15:00') |
SwiftDateは非常に強力な日付/カレンダーフレームであり、Swiftをフルに使用して記述します。 SwiftDate QuantLibパーツは、有名な量理ファイナンスのライブラリに基づいており、SwiftDateは非常に使いやすい金融アプリケーションですが、強力なビジネスカレンダー機能も備えています。
作成日:
.isToday() // true if represented date is today
.isTomorrow()
.isYesterday()
.isThisWeek() // true if represented date's week is the current week
.isSameWeekOf(date: NSDate) // true if two dates share the same year's week
.dateAtWeekStart() // return the date where current's date week starts
.beginningOfDay() // return the same date of the sender with time set to 00:00:00
.endOfDay() // return the same date of the sender with time set to 23:59:59
.beginningOfMonth() // return the date which represent the first day of the sender date's month
.endOfMonth() // return the date which represent the last day of the sender date's month
.beginningOfYear() // return the date which represent the first day of the sender date's year
.endOfYear() // return the date which represent the last day of the sender date's year
.isWeekday() // true if current sender date is a week day
.isWeekend() // true if current sender date is a weekend day (sat/sun)
簡単な計算日:
let string = date.toString(format: DateFormat.Custom('YYYY-MM-DD'))
複雑な日付変換関数:
let string = date.toString(dateStyle: .ShortStyle timeStyle:.LongStyle relativeDate:true)
カレンダー
.toISOString() // DateFormat.ISO8601
.toShortString() // short style, both time and date are printed
.toMediumString() // medium style, both time and date are printed
.toLongString() // full style, both time and date are printed
.toShortDateString() // short style, print only date
.toShortTimeString() // short style, print only time
.toMediumDateString() // medium style, print only date
.toMediumTimeString() // medium style, print only time
.toLongDateString() // long style, print only date
.toLongTimeString() // long style, print only time
日付が計算されます:
var d = NSDate()-2.hour
var abb = d.toRelativeString(abbreviated: true, maxUnits: 3)
println('data: (abb)')
プロジェクトのホームページ:
Swift日付/カレンダーフレームワーク:SwiftDate
Github
SwiftDate
SwiftDateバージョン2.0はSwiftをサポートします
参照リンク:
1.https://www.aswifter.com/2015/07/26/use-swiftdate/
2.http://www.faceye.net/search/180350.html#bottom-ad