2

我基本上想将特定日期(x)转换为前一天日期(x - 1 天)以及第二天日期(x + 1 天)。我为此使用以下代码:

NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];

但是我的日期 (x) 是 NSString 格式,在应用上述代码之前,我需要将 NSString( myDateString) 转换为 NSDate( )。myDate我有一个 NSString 包含格式为 MM-dd-yyyy 的日期。为了进行转换,我使用了以下代码,但我得到了荒谬的值。

 NSLog(@"myDateString=%@",myDateString);//output:myDateString=10-25-2012//all correct
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy"];
NSDate *myDate=[formatter dateFromString:myDateString];
NSLog(@"myDate=%@",myDate);//output:myDate=2012-10-24 18:30:00 +0000 // I only want the date to be shown // and why has the format changed
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
NSLog(@"datePlusOneDay=%@",datePlusOneDay);//output:datePlusOneDay=2012-10-25 18:30:00 +0000// I only want the date to come , not time // and why has the format changed

稍后我需要将 NSDate 转换为 NSString

NSString *curentString=[formatter stringFromDate:datePlusOneDay];
NSLog(@"curentString=%@",curentString); //output:curentString=10-26-2012

同样,我也想获得上一个日期。请帮助伙计们!和嗬嗬圣诞快乐!

4

6 回答 6

9

做为:componets.day = 1获得下一个,-1为前一天。

NSDate *date = [NSDate date]; // your date from the server will go here.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];

NSLog(@"newDate -> %@",newDate);
于 2012-12-25T09:00:16.010 回答
4

下面的代码应该让你知道上一个日期:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:-1]; // replace "-1" with "1" to get the datePlusOneDay
NSDate *dateMinusOneDay = [gregorian dateByAddingComponents:offsetComponents toDate:myDate options:0];

圣诞快乐 :)

于 2012-12-25T08:46:27.020 回答
1
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];

[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);

[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
于 2012-12-25T08:52:01.743 回答
1

在上一个日期尝试这个简单的解决方案:-

NSDate *datePlusOneDay = [[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 24)];
NSLog(@"datePlusOneDay=%@",datePlusOneDay);
于 2012-12-25T09:04:59.540 回答
1

斯威夫特 5 解决方案

let calendar = Calendar.current
calendar.date(byAdding: .day, value: -2, to: Date())
于 2019-04-09T06:56:02.667 回答
0

前一天的功能

-(void)previousDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =-1;
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    EndingDate = [dateFormat stringFromDate:sevenDays];

}

第二天的功能

-(void)nextDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =1;
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    EndingDate = [dateFormat stringFromDate:sevenDays];

}
于 2015-04-30T12:31:38.473 回答