iOS规范化时间格式,object-C计算指定时间与当前的时间差

object-c计算指定时间与当前的时间差

头文件(.h):

#import <Foundation/Foundation.h>

@interface LuDate : NSDate
+(NSString *) compareCurrentTime:(NSString*) strDate;
@end

.m文件:

/**

 * 计算指定时间与当前的时间差

 * @param compareDate   某一指定时间

 * @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前)

 */

+(NSString *) compareCurrentTime:(NSString*) strDate

{

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

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

    NSDate *compareDate= [dateFormatter dateFromString:strDate];

    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];

    timeInterval = -timeInterval;

    long temp = 0;

    NSString *result;

    if (timeInterval < 60) {

        result = [NSString stringWithFormat:@"刚刚"];

    }

    else if((temp = timeInterval/60) <60){

        result = [NSString stringWithFormat:@"%ld分前",temp];

    }

    else if((temp = temp/60) <24){

        result = [NSString stringWithFormat:@"%ld小前",temp];

    }

    else if((temp = temp/24) <30){

        result = [NSString stringWithFormat:@"%ld天前",temp];

    }

    else if((temp = temp/30) <12){

        result = [NSString stringWithFormat:@"%ld月前",temp];

    }

    else{

        temp = temp/12;

        result = [NSString stringWithFormat:@"%ld年前",temp];

    }

    return  result;

}
@end
时间: 2024-08-28 20:49:39

iOS规范化时间格式,object-C计算指定时间与当前的时间差的相关文章

js中使用eval()方法将字符串转换成日期格式、并获取指定时间的日期

1.在js中eval()方法将字符串格式数据转换成日期格式 function getDate(strDate) {         //strDate为需要转换成日期格式的字符串         var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,                 function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');    

计算指定时间与当前的时间差 比如,3天前、10分钟前

计算指定时间与当前的时间差  比如,3天前.10分钟前(这个在项目中经常遇到,所以记录了下来) 以下是实现方法: /** * 计算指定时间与当前的时间差 * @param compareDate   某一指定时间 * @return 多少(秒or分or天or月or年)+前 (比如,3天前.10分钟前) */ +(NSString *) compareCurrentTime:(NSDate*) compareDate // { NSTimeInterval  timeInterval = [com

将json的时间格式转换成正常的时间格式

/** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) 可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 * (new Date())

Newtonsoft.Json在转换指定时间格式时默认是UTC时间

最近做项目发现前台传入时间格式是/Date(1542893033449)/的时候,后台用Newtonsoft.Json发现竟然转换成了UTC时间,查阅了Newtonsoft.Json文档,发现如果需要转换成本地时区的时间需要按照以下写法 public void SetDate(string piple) { var x = new JsonSerializerSettings(); x.DateTimeZoneHandling = DateTimeZoneHandling.Local; var

如何解决前端传来的时间格式与mysql表中时间格式不匹配的查询问题

前端传过来的时间格式为“2016-07-11 11:13:10”,而数据表中对应字段`add_time`的格式为“2016-7-11”,此时sql不能直接用 "where `add_time` = '2016-07-11 11:13:10'.可以通过下面方式实现: 1. 在用于sql查询之前用PHP的strtotime()和date()对数据进行格式化,成为最终需要的格式,再将数据用于查询: 1 $t = '2015-03-02 12:04:12'; 2 $t = date('Y-m-d', s

如何把calendar.getTimeInMillis()获得的毫秒时间,已经写入数据库了而且转化为了字符串型例如:1229073608937样式的时间格式化为yyyy-MM-dd样式的时间?

一. long time = 1229073608937L;Date date = new Date(time);java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd");String timestring = f.format(date); 二. String str = String.format("%tF %<tT", 1229159619623L);

js求指定时间的周一和周日

/*计算指定时间的的周一和周日 return=>{mondy:Date,sundy:Date} parms:{ date:指定时间,如果不指定则取当前时间 } */ function getWeekArea(date) { if (!(date instanceof Date)) { date = new Date(); } var day = date.getDay(), _date = date.getDate(), mondy = _date - (day == 0 ? 7 : day)

NSdate 时间格式

NSdate 时间格式 NSTimeInterval 时间间隔 基本单位 秒 NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象 日期格式如下: y  年 M  年中的月份 D  当天是今年的第多少天 d  月份中的天数 F  月份中的周数 E  星期几 a  Am/pm H  一天中的小时数(0-23) k  一天中的小时数(1-24) K  am/pm 中的小时数(0-11)  Number  0 h  am/pm 中的小时数(1-12)  Number 

DEDE日期时间格式大全

打造最全的CMS类教程聚合! 日期时间格式 (利用strftime()函数格式化时间) 首页: ([field:pubdate function='strftime("%m-%d",@me)'/])==(5-15) ([field:pubdate function='strftime("%b %d, %Y",@me)'/])==(May 15, 2008) 列表页: [field:pubdate function="GetDateTimeMK(@me)&q