iOS 本地時間 / UTC時間 / 時間戳等操作 / 獲取當前年月日

来源:http://www.cnblogs.com/lurenq/archive/2017/06/13/6895202.html
-Advertisement-
Play Games

//獲得當前時間並且轉為字元串 //獲取當前時間轉為時間戳 //13位時間戳1469193006001(毫秒)轉 系統時間2016-08-11 08:55:36 如果只獲取當前的年月日,用NSDate 直接截取是不對的,以下方法提供了獲取當前的年月日等等 ...


 //獲得當前時間並且轉為字元串

複製代碼
- (NSString *)dateTransformToTimeString
{
    NSDate *currentDate = [NSDate date];//獲得當前時間為UTC時間 2014-07-16 07:54:36 UTC  (UTC時間比標準時間差8小時)
    //轉為字元串
    NSDateFormatter*df = [[NSDateFormatter alloc]init];//實例化時間格式類
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//格式化
    //2014-07-16 07:54:36(NSString類)
    NSString *timeString = [df stringFromDate:currentDate];
    return timeString;
}
複製代碼

 //獲取當前時間轉為時間戳

- (NSString *)dateTransformToTimeSp
{
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;//客戶端當前13位毫秒級時間戳
    NSString *timeSp = [NSString stringWithFormat:@"%llu",recordTime];//時間戳轉字元串(13位毫秒級時間戳字元串)
    return timeSp;
}
複製代碼
 1 //時間戳字元串1469193006001(毫秒)1469193006.001(毫秒,1469193006001234(微秒)1469193006.001234(微秒)轉 UTC時間2016-08-11T07:00:55.611Z
 2 - (NSString *)timespToUTCFormat:(NSString *)timesp
 3 {
 4     NSString *timeString = [timesp stringByReplacingOccurrencesOfString:@"." withString:@""];
 5     if (timeString.length >= 10) {
 6         NSString *second = [timeString substringToIndex:10];
 7         NSString *milliscond = [timeString substringFromIndex:10];
 8         NSString * timeStampString = [NSString stringWithFormat:@"%@.%@",second,milliscond];
 9         NSTimeInterval _interval=[timeStampString doubleValue];
10         NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
11 
12         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
13         NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
14         [dateFormatter setTimeZone:timeZone];
15         [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
16         NSString *dateString = [dateFormatter stringFromDate:date];
17 
18         return dateString;
19     }
20     return @"";
21 }
複製代碼

//13位時間戳1469193006001(毫秒)轉 系統時間2016-08-11 08:55:36

複製代碼
 1 + (NSString *)timespToYMDFormat:(NSNumber *)timesp
 2 {
 3     NSString *stime = [timesp stringValue];
 4     NSTimeInterval time = [[stime substringToIndex:10] doubleValue];
 5     NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
 6     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 7     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 8 
 9     return [dateFormatter stringFromDate: detaildate];
10 }
複製代碼

//時間轉時間戳的方法:sendDate為NSDate類
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[sendDate timeIntervalSince1970]];

 如果只獲取當前的年月日,用NSDate 直接截取是不對的,以下方法提供了獲取當前的年月日等等

 // 獲取代表西曆的NSCalendar對象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 獲取當前日期
    // 定義一個時間欄位的旗標,指定將會獲取指定年、月、日、時、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 獲取不同時間欄位的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:localeDate];
    NSInteger year = comp.year;

//下麵是可以獲取的內容 //
@property NSInteger era;
@property NSInteger year;
@property NSInteger month;
@property NSInteger day;
@property NSInteger hour;
@property NSInteger minute;
@property NSInteger second;
@property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekday;
@property NSInteger weekdayOrdinal;
@property NSInteger quarter NS_AVAILABLE(10_6, 4_0);
@property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0);
@property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0);

 


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

-Advertisement-
Play Games
更多相關文章
  • 一、 基本概述 下麵列出咖啡、茶的沖泡方法。 1.咖啡沖泡方法 (1) 把水煮沸 (2) 用沸水沖泡咖啡 (3) 把咖啡倒進杯子 (4) 家牛奶和糖 2.茶的沖泡方法 (1) 把水煮沸 (2) 用沸水浸泡茶葉 (3) 把茶倒進杯子 (4) 加檸檬 在使用代碼來完成這些方法時,我們一般想到的創建2個類 ...
  • 那對繼承,我是這麼看的。咱們這個繼承一般是針對父方法和子方法來說的,也就是說有兩個方法,一個父方法,一個子方法。子方法可以擁有父方法的所有屬性以及函數,那對於我們來說,js中常用的繼承有3種。 第一種原型鏈繼承:子對象他有一個預設屬性是叫__proto__,他是指向這個方法本身,而方法本身他是不帶父 ...
  • HDHaoShaoPengdeiMac:~ hdhaoshaopeng$ defaults write com.apple.finder AppleShowAllFiles TRUE HDHaoShaoPengdeiMac:~ hdhaoshaopeng$ killall Finder 以上命令是查 ...
  • 一 第一種方法 在select 標簽的父標簽加上 data-tap-disabled=”true” 二 找到 ionic.bundle.js文件 下邊有個方法 function tapIgnoreEvent(e) { if (e.isTapHandled) return true; e.isTapH ...
  • 一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h RootViewController.m ...
  • 一、自己封裝URLConnection 連接請求類 public void downloadFile1() { try{ //下載路徑,如果路徑無效了,可換成你的下載路徑 String url = "http://c.qijingonline.com/test.mkv"; String path = ...
  • Android學習筆記目錄 ...
  • 前不久 項目 終於成功發佈上線了,最近就在給項目做優化,併排除一些線上軟體的 bug,因為項目中使用了友盟統計,所以在友盟給出的錯誤信息統計中能比較方便的找出客戶端異常的信息,可是很多像數組越界卻只給出了 *** -[__NSArrayM objectAtIndex:]: index 50 beyo ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...