zoukankan      html  css  js  c++  java
  • NSDate和NSString的转换及判定是昨天,今天,明天

    用于uidate,picker。。

    +(NSDate*) convertDateFromString:(NSString*)uiDate
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
        [formatter setDateFormat:@"yyyy年MM月dd日"];
        NSDate *date=[formatter dateFromString:uiDate];
        return date;
    }

    -(NSString *)getCurrentTime:(NSDate *)date{

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

        [formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];

        NSString *dataTime = [formatter stringFromDate:date];

        return dataTime;

    }

    //判定是昨天,今天,明天

    -(NSString *)compareDate:(NSDate *)date{

     

        NSTimeInterval secondsPerDay = 24 * 60 * 60;

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

        NSDate *tomorrow, *yesterday;

     

        tomorrow = [today dateByAddingTimeInterval: secondsPerDay];

        yesterday = [today dateByAddingTimeInterval: -secondsPerDay];

     

    // 10 first characters of description is the calendar date:

        NSString * todayString = [[today description] substringToIndex:10];

        NSString * yesterdayString = [[yesterday description] substringToIndex:10];

        NSString * tomorrowString = [[tomorrow description] substringToIndex:10];

     

        NSString * dateString = [[date description] substringToIndex:10];

     

        if ([dateString isEqualToString:todayString])

        {

            return @"今天";

        } else if ([dateString isEqualToString:yesterdayString])

        {

            return @"昨天";

        }else if ([dateString isEqualToString:tomorrowString])

        {

            return @"明天";

        }

        else

        {

            return dateString;

        }

    }

    输入的日期字符串形如:@"1992-05-21 13:08:08"

    - (NSDate *)dateFromString:(NSString *)dateString{

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

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

    NSDate *destDate= [dateFormatter dateFromString:dateString];

    [dateFormatter release];

    return destDate;

    }

    - (NSString *)stringFromDate:(NSDate *)date{

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; 

    //zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。

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

    NSString *destDateString = [dateFormatter stringFromDate:date];

    [dateFormatter release];

    return destDateString;

    }

    - (IBAction)handleDidPressBtn1{

    NSLog(@"%@/n", [selfdateFromString:@"2011-08-09 12:02:01"]);

    NSLog(@"%@/n", [selfstringFromDate:[selfdateFromString:@"2011-08-09 12:02:01"]]);

    }

    输出结果如下:

    2011-08-09 04:02:01 +0000

    2011-08-09 12:02:01 GMT+08:00

  • 相关阅读:
    linux sysfs (2)
    微软——助您启动云的力量网络虚拟盛会
    Windows Azure入门教学系列 全面更新啦!
    与Advanced Telemetry创始人兼 CTO, Tom Naylor的访谈
    Windows Azure AppFabric概述
    Windows Azure Extra Small Instances Public Beta版本发布
    DataMarket 一月内容更新
    和Steve, Wade 一起学习如何使用Windows Azure Startup Tasks
    现实世界的Windows Azure:与eCraft的 Nicklas Andersson(CTO),Peter Löfgren(项目经理)以及Jörgen Westerling(CCO)的访谈
    正确使用Windows Azure 中的VM Role
  • 原文地址:https://www.cnblogs.com/nenhallgg/p/5504823.html
Copyright © 2011-2022 走看看