IOS-時間與字符串互相轉換


https://blog.csdn.net/u012605210/article/details/37879511

有時會遇到這樣的問題,需要把時間和時間戳互相轉換

比如把當前時間轉換成 “20140716155436”這樣的格式

或者是把“20140716155436”轉換成“2014-07-16 15:54:36”

首先來第一個:

當前時間轉換成 “20140716155436”這樣的格式

 

 ///////////////////////////////

    //獲取當前時間

    NSDate * today = [NSDate date];

    NSLog(@"%@",today);

打印出來是“2014-07-16 07:54:36 +0000”

很奇怪?現在明明是2014-07-16 15:54:36啊

因為存在不同時區,系統默認格林尼治時間

所以就要轉換時間格式了

    //轉換時間格式

    NSDateFormatter *df = [[NSDateFormatter alloc] init];//格式化

    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString * s1 = [df stringFromDate:today];

    NSLog(@" s1s1s1s%@",s1);

打印出來是“2014-07-16 15:54:36”

現在已經獲取到當前的時間

這樣還沒有完呢,需要轉換成一個字符串,類似“20140716052021”的時間戳

 

    NSDate * date = [df dateFromString:s1];

    //轉換時間格式

    NSDateFormatter *df2 = [[NSDateFormatter alloc] init];//格式化

    [df2 setDateFormat:@"yyyyMMddHHmmss"];

    [df2 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];

    NSLog(@"%@",[df2 stringFromDate:date]);

打印出來是“20140716155436”
 

接下來就是第二個:

把“20140716155436”轉換成“2014-07-16 15:54:36”

首先要把20140716155436轉換成時間格式

 

    //轉換時間格式

    NSDateFormatter *df = [[NSDateFormatter alloc] init];//格式化

    [df setDateFormat:@"yyyyMMddHHmmss"];

    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] ];

    NSDate *date =[[NSDate alloc]init];

    date =[df dateFromString:bill_timeStr];

    str = [NSString stringWithFormat:@"%@",date];

str的結果是“2014-07-16 07:54:36 +0000”

這時候就需要再轉一次

    //

    NSDateFormatter * df2 = [[NSDateFormatter alloc] init];

    [df2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString * str1 = [df2 stringFromDate:date];

 str1的結果是“2014-07-16 15:54:36”

ok,大功告成

 

例1、 從服務器段獲取到的字符串轉化為時間如:轉化1416882712000

//網絡請求獲取的數據

NSString *time = [NSStringstringWithFormat:@%@,[[dateListobjectAtIndex:indexPath.row]gradeDate]];

NSInteger num = [time integerValue]/1000;(重點)

NSDateFormatter *formatter = [[[NSDateFormatteralloc]init]autorelease];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:@YYYY-MM-dd];

NSDate*confromTimesp = [NSDatedateWithTimeIntervalSince1970:num];NSString*confromTimespStr = [formatterstringFromDate:confromTimesp];

cell.DateContent.text = confromTimespStr;

轉化之后結果為:2014-11-25

 

例2、如何如何將一個字符串如“ 20110826134106”裝化為任意的日期時間格式,下面列舉兩種類型

NSString*string = @20110826134106;

NSDateFormatter *inputFormatter= [[[NSDateFormatter alloc] init] autorelease];

[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@en_US] autorelease]];

[inputFormatter setDateFormat:@yyyyMMddHHmmss];

NSDate*inputDate = [inputFormatter dateFromString:string];

NSLog(@date= %@, inputDate);

 

NSDateFormatter *outputFormatter= [[[NSDateFormatter alloc] init] autorelease];

[outputFormatter setLocale:[NSLocale currentLocale]];

[outputFormatter setDateFormat:@yyyy年MM月dd日 HH時mm分ss秒];

NSString *str= [outputFormatter stringFromDate:inputDate];

NSLog(@testDate:%@,str);

兩次打印的結果為:

date= 2011-08-26 05:41:06 +0000

testDate:2011年08月26日 13時41分06秒

 

iOS時間的時區轉換以及一些方法記錄

https://www.jianshu.com/p/b39852ddbf0c

2016.09.20 13:54* 字數 48 閱讀 5654評論 8

系統的一些方法

    NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMT]; [NSTimeZone systemTimeZone];//系統所在時區 [NSTimeZone defaultTimeZone];//默認時區,貌似和上一個沒什么區別 [NSTimeZone timeZoneForSecondsFromGMT:0];//這就是GMT+0時區了 [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*60*60]];//直接指定時區,這里是東8區 

將時間戳轉換成時間

    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval]; NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init]; [objDateformat setDateFormat:@"yyyy-MM-dd HH:mm"]; self.timeStr = [objDateformat stringFromDate:date]; 

將0時區時間轉換成本地時區的時間

    [formatter setTimeZone:[NSTimeZone systemTimeZone]]; 

將字符串轉換為date

- (NSDate *)stringToDate:(NSString *)strdate { NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *retdate = [dateFormatter dateFromString:strdate]; [dateFormatter release]; return retdate; } 

將date轉換為字符串

- (NSString *)dateToString:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *strDate = [dateFormatter stringFromDate:date]; [dateFormatter release]; return strDate; }

iOS中NSDate和NSString相互轉換

https://blog.csdn.net/whuizhou/article/details/46753393

本文來源地址:http://www.osjoin.com 

 

今天給大家分享一些關於NSDate和NSString相互轉換。

 

 

日期轉成字符串。這個雖然簡單,但是我相信很多朋友初次遇到肯定束手無策。腦子里蹦出四個字:這怎么轉?直接上代碼:

 

  1.  
    //獲取系統當前時間
  2.  
    NSDate *currentDate = [NSDate date];
  3.  
    //用於格式化NSDate對象
  4.  
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  5.  
    //設置格式:zzz表示時區
  6.  
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
  7.  
    //NSDate轉NSString
  8.  
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
  9.  
    //輸出currentDateString<span style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif; line-height: 25.6000003814697px; white-space: pre-wrap;">NSLog(@"%@",currentDateString);</span>

 

NSDate對象包含兩個部分,日期(Date)和時間(Time)。格式化的時間字符串主要也是針對日期和時間的。NSDateFormatter是一個很常用的類,用於格式化NSDate對象,支持本地化的信息。

 

常用的轉換格式為:

yyyy-MM-dd HH:mm:ss.SSS

yyyy-MM-dd HH:mm:ss

yyyy-MM-ddMM dd yyyy

NSDateFormatter的轉換格式:

 

  1.  
    G: 公元時代,例如AD公元
  2.  
    yy: 年的后 2位
  3.  
    yyyy: 完整年
  4.  
    MM: 月,顯示為 1-12
  5.  
    MMM: 月,顯示為英文月份簡寫,如 Jan
  6.  
    MMMM: 月,顯示為英文月份全稱,如 Janualy
  7.  
    dd: 日, 2位數表示,如02
  8.  
    d: 日, 1-2位顯示,如 2
  9.  
    EEE: 簡寫星期幾,如Sun
  10.  
    EEEE: 全寫星期幾,如Sunday
  11.  
    aa: 上下午,AM/PM
  12.  
    H: 時, 24小時制,0-23
  13.  
    K:時, 12小時制,0-11
  14.  
    m: 分, 1-2位
  15.  
    mm: 分, 2位
  16.  
    s: 秒, 1-2位
  17.  
    ss: 秒, 2位
  18.  
    S: 毫秒

 

既然NSDate可以轉成NSString,毫無疑問NSString也可以轉成NSDate。代碼如下:

 

  1.  
    /**
  2.  
    * 利用nsdateformatter 轉換時間
  3.  
     
  4.  
    把當前時間轉換為字符串
  5.  
    */
  6.  
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  7.  
    [formatter setDateFormat: @"yyyy-MM-dd HH:mm:sss"];
  8.  
    //獲取當前時間
  9.  
    NSDate *dateNow = [NSDate date];
  10.  
    NSLog(@"%@",[formatter stringFromDate:dateNow]);

 

 

  1.  
    /**
  2.  
    * 把字符串時間轉換為nsdate
  3.  
    */
  4.  
     
  5.  
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
  6.  
    [formatter setDateFormat: @"yyyy-MM-dd HH-mm-sss"];
  7.  
     
  8.  
    NSDate *resDate = [formatter dateFromString:@"2014-10-1 12-00-00"];
  9.  
    NSLog(@"%@",resDate);

 

 

 

 轉換結論:

   

把一個nsdate類型轉換成string類型的時間格式的時候,會在零時區的基礎上加8個小時

把一個string類型時間格式轉換成nsdate格式的時候,會把時間回歸到零時區記時

在轉換過程中,系統會自動幫你計算。人為的好像還不能限制。

 

 

在轉換時間的時候一定要把占位符 寫好

     以及各個時間長度的占位符。不然就會得到的就是null。

 

 

轉換工具類

 

在項目中,我們需要用到轉換的地方可能不止一處,所以建議我們定義一個工具類。在工具類里實現如下兩個方法:

 

 

  1.  
    //NSDate轉NSString
  2.  
    + ( NSString *)stringFromDate:(NSDate *)date
  3.  
    {
  4.  
    //獲取系統當前時間
  5.  
    NSDate *currentDate = [NSDate date];
  6.  
    //用於格式化NSDate對象
  7.  
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  8.  
    //設置格式:zzz表示時區
  9.  
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
  10.  
    //NSDate轉NSString
  11.  
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
  12.  
    //輸出currentDateString
  13.  
    NSLog(@"%@",currentDateString);
  14.  
    return currentDateString;
  15.  
    }
  16.  
     
  17.  
    //NSString轉NSDate
  18.  
    + ( NSDate *)dateFromString:(NSString *)string
  19.  
    {
  20.  
    //需要轉換的字符串
  21.  
    NSString *dateString = @"2015-06-26 08:08:08";
  22.  
    //設置轉換格式
  23.  
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
  24.  
    [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
  25.  
    //NSString轉NSDate
  26.  
    NSDate *date=[formatter dateFromString:dateString];
  27.  
    return date;
  28.  
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM