在 iOS 中将日期转换为时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21130616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:08:14  来源:igfitidea点击:

Convert date to timestamp in iOS

iosdatensdatenstimeinterval

提问by user2799156

How do you convert any given date to milliseconds? For example, 2014-01-23 to timestamp conversion.

你如何将任何给定的日期转换为毫秒?例如,2014-01-23 到时间戳转换。

NSDate *date = [dateFormatter dateFromString:@"2014-01-23"];
NSLog(@"date=%@",date);
NSTimeInterval interval  = [date timeIntervalSince1970];
NSLog(@"interval=%f",interval);
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval];
[dateFormatter setDateFormat:@"yyyy/mm/dd "];
NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]);

Output result: 1970/30/01

输出结果:1970/30/01

回答by Suragch

Swift

迅速

Convert the current date/time to a timestamp:

将当前日期/时间转换为时间戳:

// current date and time
let someDate = Date()

// time interval since 1970
let myTimeStamp = someDate.timeIntervalSince1970

See also

也可以看看

回答by KudoCC

Have it a try. "mm" stands for minute while "MM" stands for month.

试一试。“mm”代表分钟,而“MM”代表月份。

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"yyyy-MM-dd"] ;
NSDate *date = [dateFormatter dateFromString:@"2014-01-23"] ;
NSLog(@"date=%@",date) ;
NSTimeInterval interval  = [date timeIntervalSince1970] ;
NSLog(@"interval=%f",interval) ;
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval] ;
[dateFormatter setDateFormat:@"yyyy/MM/dd "] ;
NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]) ;

回答by Alex Brown

NSTimeinterval is really a double, being seconds since a particular date (in your case, the start of 1970)

NSTimeinterval 确实是一个双倍,是自特定日期以来的秒数(在您的情况下,是 1970 年的开始)

回答by Xeieshan

[date1 timeIntervalSinceDate:date2]will return seconds from two NSDate objects.

[date1 timeIntervalSinceDate:date2]将从两个 NSDate 对象返回秒。

double seconds = [date1 timeIntervalSinceDate:date2];
double milliSecondsPartOfCurrentSecond = seconds - [seconds intValue];

milliSecondsPartOfCurrentSecond

毫秒部分当前秒

回答by Vicky

NOTE :- UNIX Timestamp format contains 13 Digits so we need a 1000 multiplication with the result. And the timestamp must be UTC ZONE not our local Time Zone.

注意:- UNIX 时间戳格式包含 13 位数字,因此我们需要与结果相乘 1000。并且时间戳必须是 UTC ZONE 而不是我们当地的时区。

- (void)GetCurrentTimeStamp
    {
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyy-MM-dd"];
        NSString    *strUTCTime = [self GetUTCDateTimeFromLocalTime:@"2014-01-23"];
        NSDate *objUTCDate  = [objDateformat dateFromString:strUTCTime];
        long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);
        NSLog(@"Local Time = %@---- UTC = %@ ----- TimeSatmp = %ld  ---- TimeStamp = %lld",strTime,strUTCTime,unixTime,milliseconds);
    }

 - (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate  *objDate    = [dateFormatter dateFromString:IN_strLocalTime];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSString *strDateTime   = [dateFormatter stringFromDate:objDate];
        return strDateTime;
    }