IOS时间戳合集,处理日期的功能方法

时间戳合集~
 
+ (NSString *)TimerString:(float)fTimer
{
    int iHour = fTimer / 3600;
    float fMinute = fTimer - (iHour * 3600);
    int iMinute = fMinute / 60;
    int iSecond = fMinute - (iMinute * 60);
     
    if (iHour != 0)
    {
        return [NSString stringWithFormat:@"%.2d:%.2d:%.2d", iHour, iMinute, iSecond];
    }
    else
    {
        return [NSString stringWithFormat:@"%.2d:%.2d", iMinute, iSecond];
    }
    return nil;
}
 
+ (NSString *)GetCurrentDate
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSString *date = [[NSString alloc] initWithString:[dateFormatter stringFromDate:now]];
    [dateFormatter release];
    return [date autorelease];
}
+ (NSString *)GetPureCurrentDate
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMMdd";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSString *date = [[NSString alloc] initWithString:[dateFormatter stringFromDate:now]];
    [dateFormatter release];
    return [date autorelease];
}
 
+ (NSString *)GetCurrentTimeForFileSuffix
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd_HH-mm-ss_SSS";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSString *date = [[NSString alloc] initWithString:[dateFormatter stringFromDate:now]];
    [dateFormatter release];
    return [date autorelease];
}
 
+ (NSString *)GetCurrentTimeStr
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSString *date = [[NSString alloc] initWithString:[dateFormatter stringFromDate:now]];
    [dateFormatter release];
    return [date autorelease];
}
 
+ (NSDate *)GetCurrentTime
{
    NSDate *now = [NSDate date];
    return now;
}
 
+ (NSTimeInterval)GetCurrentGMTTime
{
    NSDate *now = [NSDate date];
    NSTimeInterval nowTime = [now timeIntervalSince1970];
    return nowTime;
}
 
+ (NSTimeInterval)GetCurrentMillisecondGMTTime
{
    NSTimeInterval nowTime = [self GetCurrentGMTTime] * 1000;
    NSTimeInterval _nowTime = nowTime/1000;
    return _nowTime;
}
 
+ (NSString *)GetCurrentTimeStringWithFormat:(NSString *)format
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = format;
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSString *date = [[NSString alloc] initWithString:[dateFormatter stringFromDate:now]];
    [dateFormatter release];
    return [date autorelease];
}
 
+ (NSString*)setMonth
{
    NSDateFormatter* nowTime = [[NSDateFormatter alloc]init];
    [nowTime setDateFormat:@"y-MM"];
    NSString* month = [NSString stringWithFormat:@"%@",[nowTime stringFromDate:[NSDate date]]];
    [nowTime release];
    return month;
}
 
+ (NSString*)setLastMonth:(NSString *)monthStr
{
    NSString *tempY = [monthStr substringToIndex:4];
    NSString *tempM = [monthStr substringFromIndex:5];
     
    int tmpY = [tempY intValue];
    int tmpM = [tempM intValue];
     
    if (1 == tmpM) {
        tmpY--;
        tmpM = 12;
    }
    else {
        tmpM--;
    }
   NSString* lastMonth = [NSString stringWithFormat:@"%d-%02d",tmpY,tmpM];
   return lastMonth;
}
 
+ (NSDate *)GetDateWithTimeString:(NSString *)time
{
    if (time == nil) 
    {
        return nil;
    }
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:time];
    return date;
}
 
+ (NSDate *)GetDateWithTimeString:(NSString *)time format:(NSString *)format
{
    if (time == nil){
        return nil;
    }
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:time];
    return date;
}
+ (NSString *)getDateStringTimeInterval:(unsigned long long)timeInterval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [formatter stringFromDate:date];
}
 
+ (NSDate *)GetDateWithGMT:(unsigned long long)timeInterval
{
    return [NSDate dateWithTimeIntervalSince1970:timeInterval];
}
 
+ (NSString *)GetStringWithDate:(NSDate *)time
{
    if (time == nil) 
    {
        return @"";
    }
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *date = [[formatter stringFromDate:time] retain];
    [formatter release];
    return [date autorelease];
}
 
+ (NSString *)GetStringWithDate:(NSDate *)time format:(NSString *)format
{
    if (time == nil)
    {
        return @"";
    }
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *date = [[formatter stringFromDate:time] retain];
    [formatter release];
    return [date autorelease];
}
 
+ (NSString *)GetLocalStringWithDate:(NSDate *)time format:(NSString *)format
{
 
    NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
    fmt.locale = [NSLocale currentLocale];
 
    [fmt setDateStyle:NSDateFormatterShortStyle];
    [fmt setTimeStyle:NSDateFormatterShortStyle];
//    if (format) {
//        fmt.dateFormat = format;
//    }else{
//        fmt.dateFormat = @"yyyy-MM-dd HH:mm a";
//    }
    NSString* dateString = [fmt stringFromDate:time];
    return dateString;
}
 
+ (NSString *)GetStringHourWithDate:(NSDate *)time
{
    if (time == nil) 
    {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"h:mm a"];
    NSString *date = [formatter stringFromDate:time];
    NSString *ret = @"  ";
    if (date != nil) 
    {
        ret = [[NSString alloc] initWithString:date];
    }
     
    [formatter release];
    return [ret autorelease];
}
 
+ (NSDate *)ConvertDatetoLocal:(NSDate *)date
{
    if (date == nil)
    {
        return nil;
    }
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval]; 
    return localeDate;
}
+ (NSDate *)ConvertGMTTimeToLocal:(NSTimeInterval)timeinterval
{
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSDate *date = [TimeHelper GetDateWithGMT:timeinterval];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    return localeDate;
}
 
+(NSString *)getFormateTimeWithDate:(NSDate *)timeDate
{
    if (timeDate == nil)
    {
        return @" ";
    }
    NSDate *currentlocaleDate           = [TimeHelper GetCurrentTime];  // 当前时间
     
    long long  todayInterval            = [currentlocaleDate timeIntervalSince1970];
    currentlocaleDate                   = [currentlocaleDate dateByAddingTimeInterval:-(todayInterval%86400)];
    todayInterval                       = [currentlocaleDate timeIntervalSince1970];
    NSDateFormatter *dateFormatter      = [[[NSDateFormatter alloc]init] autorelease];
    [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
    NSString *dateStr                   = [dateFormatter stringFromDate:timeDate];
     
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSDate *date                        = [dateFormatter dateFromString:dateStr]; //要转换的时间
    long long  dateInterval             = [date timeIntervalSince1970];
    int intervalDay                     = (long long)(todayInterval - (dateInterval-dateInterval%86400))/86400;
     
    if (intervalDay < 1 && intervalDay >= 0) //今天
    {
        todayInterval       = [[TimeHelper GetCurrentTime] timeIntervalSince1970];
        int todayHour       = (long long)todayInterval%86400/3600;
        int dateHour        = (long long)dateInterval%86400/3600;
        int intervalHour    = todayHour - dateHour;
        int minInterval     = (todayInterval - dateInterval)/60;
        if (intervalHour == 0 || (intervalHour == 1 && minInterval < 60))  // 1小时之内
        {
            NSString *timeStr = nil;
            if (minInterval <= 0)
            {
                return NSLocalizedString(@"Now", nil);
            }
            else
            {
                timeStr   = [NSString stringWithFormat:@"%d",minInterval];
            }
            if (minInterval > 1) {
                timeStr = [NSString stringWithFormat:@"%@ %@",timeStr,NSLocalizedString(@"mins ago", nil)];
            }else{
                timeStr = [NSString stringWithFormat:@"%@ %@",timeStr,NSLocalizedString(@"min ago", nil)];
            }
             
            return NSLocalizedString(timeStr, nil);
        }
        return [[self class] GetStringHourWithDate:timeDate];
    }
    if(intervalDay < 2)// 昨天
    {
        return NSLocalizedString(@"Yesterday", nil);
    }
     
    int weekInterval          = (todayInterval - dateInterval)/86400/7;
    if (weekInterval == 0)//一周之内
    {
        [dateFormatter setDateFormat:@"EEE, MMM d, yy"];
        NSString *str   = [dateFormatter stringFromDate:date];
        NSArray *array  = [str componentsSeparatedByString:@","];
        str = [array objectAtIndex:0];
        return NSLocalizedString(str, nil);
    }
    return [dateStr substringToIndex:10];
}
+ (NSString *)getTimeStrWithDate:(NSDate *)timeDate
{
    if (time == nil)
    {
        return @"";
    }
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    NSString *date = [[formatter stringFromDate:timeDate] copy];
    [formatter release];
    return [date autorelease];
}
 
+ (uint32_t)GenerateRandomNum
{
    if (randomSeedflag) {
        srandom((unsigned int)time(NULL));
        randomSeedflag = NO;
    }
    uint32_t ret = random();
    return ret;
     
}
 
+ (long)GenerateRandomNumLong
{
    if (randomSeedflaglong) {
        srandom((unsigned int)time(NULL));
        randomSeedflaglong = NO;
    }
    long ret = random();
    return ret;
}
 
+ (NSString *)amStringWithStr:(NSString *)dateStr
{
    if (dateStr == nil){
        return @"";
    }
     
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    if ([dateStr length] > 11) {
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }else{
        [formatter setDateFormat:@"MM-dd HH:mm"];
    }
     
    NSDate *date = [formatter dateFromString:dateStr];
    [formatter setDateFormat:@"MM-dd h:mm a"];
    NSString *str = [formatter stringFromDate:date];
    [formatter release];
    if (str) {
        return str;
    }
    return dateStr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值