iOS 獲取西曆、農曆日期的年月日

来源:http://www.cnblogs.com/silence-cnblogs/archive/2017/02/05/6368437.html
-Advertisement-
Play Games

iOS 獲取西曆、農曆日期的年月日 介紹三種方法獲取 Date (NSDate) 的年月日。 用 date 表示當前日期。測試日期為西曆 2017 年 2 月 5 日,農曆丁酉年,雞年,正月初九。 獲取西曆年月日 用 Calendar (NSCalendar) 獲取西曆年月日 結果 用 Calend ...


iOS 獲取西曆、農曆日期的年月日

介紹三種方法獲取 Date (NSDate) 的年月日。

用 date 表示當前日期。測試日期為西曆 2017 年 2 月 5 日,農曆丁酉年,雞年,正月初九。

let date: Date = Date()
NSDate *date = [NSDate date];

獲取西曆年月日

用 Calendar (NSCalendar) 獲取西曆年月日

let calendar: Calendar = Calendar(identifier: .gregorian)
print("Year:", calendar.component(.year, from: date))
print("Month:", calendar.component(.month, from: date))
print("Day:", calendar.component(.day, from: date))
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSLog(@"Year: %ld", [calendar component:NSCalendarUnitYear fromDate:date]);
NSLog(@"Month: %ld", [calendar component:NSCalendarUnitMonth fromDate:date]);
NSLog(@"Day: %ld", [calendar component:NSCalendarUnitDay fromDate:date]);

結果

用 Calendar 和 DateComponents (NSCalendar 和 NSDateComponents) 獲取西曆年月日

let componentSet: Set<Calendar.Component> = Set(arrayLiteral: .year, .month, .day)
let components: DateComponents = calendar.dateComponents(componentSet, from: date)
print("Year:", components.year!)
print("Month:", components.month!)
print("Day:", components.day!)
NSCalendarUnit calenderUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *components = [calendar components:calenderUnit fromDate:date];
NSLog(@"Year: %ld", components.year);
NSLog(@"Month: %ld", components.month);
NSLog(@"Day: %ld", components.day);

結果

用 DateFormatter (NSDateFormatter) 獲取西曆年月日

let formatter: DateFormatter = DateFormatter()
print("Date formatter identifier:", formatter.calendar.identifier) // gregorian by default
formatter.dateFormat = "y"
print("Year:", formatter.string(from: date))
formatter.dateFormat = "M"
print("Month:", formatter.string(from: date))
formatter.dateFormat = "d"
print("Day:", formatter.string(from: date))
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLog(@"Date formatter calendar: %@", formatter.calendar.calendarIdentifier); // gregorian by default
formatter.dateFormat = @"y";
NSLog(@"Year: %@", [formatter stringFromDate:date]);
formatter.dateFormat = @"M";
NSLog(@"Month: %@", [formatter stringFromDate:date]);
formatter.dateFormat = @"d";
NSLog(@"Day: %@", [formatter stringFromDate:date]);

獲取農曆年月日

用 Calendar (NSCalendar) 獲取農曆年月日

與西曆相似,更改 Calendar (NSCalendar) 的初始化即可,其他代碼相同

let calendar: Calendar = Calendar(identifier: .chinese)
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];

結果

用 Calendar 和 DateComponents (NSCalendar 和 NSDateComponents) 獲取農曆年月日

同上節用 Calendar (NSCalendar) 獲取農曆年月日

用 DateFormatter (NSDateFormatter) 獲取農曆年月日

與西曆相似,在初始化 DateFormatter (NSDateFormatter) 之後,給 calendar 屬性賦值即可,其他代碼相同

let formatter: DateFormatter = DateFormatter()
formatter.calendar = Calendar(identifier: .chinese)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];

結果

計算日期年份的生肖

自定義一個類 ChineseCalendar 來計算。十二生肖數組寫在類外面。

十二生肖數組

private let Zodiacs: [String] = ["鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬"]

ChineseCalendar 的類方法

static func zodiac(withYear year: Int) -> String {
    let zodiacIndex: Int = (year - 1) % Zodiacs.count
    return Zodiacs[zodiacIndex]
}
    
static func zodiac(withDate date: Date) -> String {
    let calendar: Calendar = Calendar(identifier: .chinese)
    return zodiac(withYear: calendar.component(.year, from: date))
}

測試

print("Chinese zodiac string:", ChineseCalendar.zodiac(withDate: date))

結果

計算日期年份的天干地支

在 ChineseCalendar 中用類方法計算。天干地支數組寫在類外面。

天干地支數組

private let HeavenlyStems: [String] = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
private let EarthlyBranches: [String] = ["子", "醜", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]

ChineseCalendar 的類方法

static func era(withYear year: Int) -> String {
    let heavenlyStemIndex: Int = (year - 1) % HeavenlyStems.count
    let heavenlyStem: String = HeavenlyStems[heavenlyStemIndex]
    let earthlyBrancheIndex: Int = (year - 1) % EarthlyBranches.count
    let earthlyBranche: String = EarthlyBranches[earthlyBrancheIndex]
    return heavenlyStem + earthlyBranche
}
    
static func era(withDate date: Date) -> String {
    let calendar: Calendar = Calendar(identifier: .chinese)
    return era(withYear: calendar.component(.year, from: date))
}

測試

print("Chinese era string:", ChineseCalendar.era(withDate: date))

結果

轉載請註明出處:http://www.cnblogs.com/silence-cnblogs/p/6368437.html



您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • (一)頁面變數對象data 對象data 有兩個方面用途 第一,前端wxml的數據渲染是通過設置此對象中定義的變數進行關聯展現的 第二,定義JS頁面中的頁面局部變數,使其整個頁面中可使用或調用 對象data定義的變數支持各種數據類型,string,int,[],{} 第一.wxml數據渲染,只要通過 ...
  • ▓▓▓▓▓▓ 大致介紹 經過前面的學習(小白學Git)已經建立了版本庫,並上傳了文件,這次來學習對這些文件進行基本的操作,即: ◆ 撤銷操作 ◆ 刪除文件 ◆ 恢覆文件 我在此之前,已經將三個文件提交到了版本庫 ▓▓▓▓▓▓ 撤銷操作 撤銷操作的語法: 撤銷操作一般有兩種情況: ◆ 文件修改後還沒有 ...
  • 可能是因為正當校招季,最近關於程式媛(女性程式員)的話題很火,朋友圈裡一下就冒出很多相關文章,有的寫自己求職入職的心路歷程,有的從客觀數據角度分析女性優劣勢,也有過來人分享自己和周圍人的看法,加之幾天前我所在的公司ThoughtWorks贏得了2016最佳女性科技人員雇主,忽然間,”程式媛”成了每天 ...
  • 一、CSS書寫順序 1.位置屬性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin)3.文字系列(font, line-height, letter-spacing, color- t ...
  • 如果是事件處理函數綁定的函數,瀏覽器會預設傳遞一個參數,而這個參數就是事件對象。 因為arguments[0]這樣使用這個參數比較麻煩,所以我們可以傳遞一個參數evt來進行使用。 ...
  • 通過JavaScript使用正則表達式驗證用戶輸入的是否為正整數。 ...
  • 這裡介紹的是大家以後要用到的html強大功能,可直接給輸入框增加語音功能,下麵我們先來看看實現方法。 大家可以看到在輸入框右邊的麥克風圖標,點擊麥克風就能夠進行語音識別了。 其實很簡單,語音識別是html5的基本功能,它的用法是 如果喜歡XHTML類似的語法,可以這樣表示 語音識別在十年前是讓人覺得 ...
  • 0x00什麼是Accessibility(輔助功能) 考慮到部分用戶不能很好地使用Android設備,比如由於視力、身體、年齡方面的限制,造成閱讀內容、觸控操作、聲音信息等方面的獲取困難,Android提供了Accessibility特性和服務幫助用戶更好地使用Android設備。 依據Androi ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...