NSDate使用大全

NSDate使用大全,个人整理的


#define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd HH:mm:ss")

//获取当前日期,时间
+(NSDate *)getCurrentDate{
    NSDate *now = [NSDate date];
    return now;
}



//将日期转换为字符串(日期,时间)
+(NSString *)getDateStringFromDate:(NSDate *)date{
    NSInteger location = 0;
    NSString *timeStr = @"";
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    [formatter setDateFormat:@"HH:mm:a"];
    NSString *ampm = [[[formatter stringFromDate:date] componentsSeparatedByString:@":"] objectAtIndex:2];
    timeStr = [formatter stringFromDate:date];
    NSRange range = [timeStr rangeOfString:[NSString stringWithFormat:@":%@",ampm]];
    location = range.location;
    NSString *string = [timeStr substringToIndex:location];
    timeStr = [NSString stringWithFormat:@"%@ %@",ampm,string];
    
    
    NSString *dateStr = @"";
    NSDateFormatter *Dformatter = [[[NSDateFormatter alloc] init] autorelease];
    [Dformatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    [Dformatter setDateFormat:@"yyyy-MM-dd"];    
    dateStr = [Dformatter stringFromDate:date];
//    NSLog(@"%@", [NSString stringWithFormat:@"%@  %@",dateStr,timeStr]);
    return [NSString stringWithFormat:@"%@  %@",dateStr,timeStr];
}




//计算两个日期之间的差距,过了多少天。。
+(NSInteger)getDateToDateDays:(NSDate *)date withSaveDate:(NSDate *)saveDate{
    NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];  
    NSUInteger unitFlags =  NSHourCalendarUnit | NSMinuteCalendarUnit |   
    NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;  
    NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:date  toDate:saveDate  options:0];  
    NSInteger diffDay   = [ cps day ];  
    [chineseClendar release]; 
    return diffDay;
}


例:NSDate *lastDate = [self getSaveDate];//saveDate通过将NSDate转换为NSString来保存
    currentDate = [NSDate date];
    NSInteger day = [DateHelper getDateToDateDays:currentDate withSaveDate: lastDate];





//日期转字符串
+ (NSString * )NSDateToNSString: (NSDate * )date
{    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
    NSString *dateString = [formatter stringFromDate:date];
    [formatter release];
    return dateString;
}


//字符串转日期
+ (NSDate * )NSStringToNSDate: (NSString * )string
{    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
    NSDate *date = [formatter dateFromString :string];
    [formatter release];
    return date;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用CFNetwork框架来实现FTP上传图片。下面是一个简单的示例代码: ``` - (void)uploadImageToFTP:(UIImage *)image { // FTP服务器地址、用户名、密码 NSString *ftpUrl = @"ftp://ftp.example.com"; NSString *userName = @"username"; NSString *password = @"password"; // 生成图片的NSData NSData *imageData = UIImageJPEGRepresentation(image, 1.0); // 生成文件名 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]]; // 生成FTP文件路径 NSString *ftpFilePath = [NSString stringWithFormat:@"%@/%@", ftpUrl, fileName]; // 生成FTP URL CFURLRef ftpURL = CFURLCreateWithString(NULL, (__bridge CFStringRef)ftpFilePath, NULL); // 生成FTP Write Stream CFWriteStreamRef writeStream = CFWriteStreamCreateWithFTPURL(NULL, ftpURL); // 设置FTP用户名和密码 CFWriteStreamSetProperty(writeStream, kCFStreamPropertyFTPUserName, (__bridge CFStringRef)userName); CFWriteStreamSetProperty(writeStream, kCFStreamPropertyFTPPassword, (__bridge CFStringRef)password); // 打开FTP Write Stream if (CFWriteStreamOpen(writeStream)) { // 写入数据 const uint8_t *buffer = [imageData bytes]; CFIndex bytesWritten = CFWriteStreamWrite(writeStream, buffer, [imageData length]); // 关闭FTP Write Stream CFWriteStreamClose(writeStream); if (bytesWritten == [imageData length]) { NSLog(@"FTP上传成功"); } else { NSLog(@"FTP上传失败"); } } else { NSLog(@"FTP连接失败"); } // 释放资源 CFRelease(writeStream); CFRelease(ftpURL); } ``` 在上面的代码中,我们首先定义了FTP服务器地址、用户名和密码,然后生成了要上传的图片的NSData、文件名、FTP文件路径和FTP URL。接着,我们使用CFWriteStreamCreateWithFTPURL函数创建了FTP Write Stream,并设置了FTP用户名和密码。然后,我们打开FTP Write Stream,并将图片数据写入到FTP服务器上。最后,我们检查数据是否写入成功,并释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值