标签: nsdate

NSDate的字符串描述

我有以下代码:

[ [NSDate date] descriptionWithLocale: @"yyyy-MM-dd" ]
Run Code Online (Sandbox Code Playgroud)

我希望它以下列格式返回日期:"2009-04-23"

但它返回:2009年4月23日星期四下午11:27:03 GMT + 03:00

我究竟做错了什么?

先感谢您.

cocoa objective-c nsdate

19
推荐指数
3
解决办法
5万
查看次数

如何在Objective-C中格式化日期,类似于jquery.timeago库?

我有一个表格单元格中显示的项目,其中一部分是过去的日期/时间戳.

在Objective-C中,如何以与网络上的jquery.timeago插件相同的方式完成格式化?

也就是说,接受日期并输出如下内容:

  • '现在'
  • '2分钟前'
  • '24天前'
  • '一个月前'

我看到这里有一个NSDate扩展类,其中包含dateWithDaysBeforeNow,dateWithMinutesBeforeNow等方法,但如果有一个已经完成此操作的库,我将使用它.

编辑:除此之外,如果某人编写了一个方法(接受一个日期,返回一个字符串)来实现这一点,无论是使用这个链接的扩展库还是其他方法,我都会给他们答案.

EDIT 2 Bounty适用于任何可以在Objective-C中编写模糊日期算法的人.

iphone objective-c nsdate ios

19
推荐指数
2
解决办法
3224
查看次数

NSDateComponent中的小时为12或24小时格式?

NSDateComponent的小时组件是12h还是24h格式?我在文档中找不到任何相关内容......

objective-c nsdate nsdatecomponents ios

19
推荐指数
1
解决办法
4063
查看次数

声音在AVAudioPlayer播放完成后执行操作?

我正在使用AVAudioPlayer框架,我有几个声音可以一次播放一个.声音播放结束后,我希望应用程序执行某些操作.我尝试使用audioPlayerDidFinishPlaying在第一个声音结束时执行操作,但我无法将其用于第二个声音,因为我遇到了重新定义错误.我可以使用NSTimeInterval来获取声音的持续时间吗?

//  ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface ViewController : UIViewController {  
UIButton        *begin;
UIWindow        *firstTestWindow;
UIWindow        *secondTestWindow;
AVAudioPlayer   *player;
NSTimeInterval  duration;
}  

@property (nonatomic, retain) IBOutlet UIButton     *begin;
@property (nonatomic, retain) IBOutlet UIWindow     *firstTestWindow;
@property (nonatomic, retain) IBOutlet UIWindow     *secondTestWindow;
@property (nonatomic, retain)         AVAudioPlayer   *player;
@property (readonly)                   NSTimeInterval  duration;


- (IBAction)begin:(id)sender;
- (IBAction)doTapScreen:(id)sender;

@end



//ViewController.m
#import "ViewController.h"

@implementation ViewController

@synthesize begin, player, firstTestWindow, secondTestWindow;

//first sound
- (IBAction)begin:(id)sender; {

    [firstTestWindow makeKeyAndVisible];
    NSString *soundFilePath =
    [[NSBundle mainBundle] pathForResource: …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch nsdate avaudioplayer

18
推荐指数
1
解决办法
1万
查看次数

我怎么能从月份号码获得月份名称?

可能重复:
iOS:如何从数字中获取正确的月份名称?

如何从月份编号获取月份名称?

我有像02这样的月份编号,但是希望月份为字符串,在这种情况下为2月.

iphone nsdate

18
推荐指数
1
解决办法
2万
查看次数

无法使用NSDateFormatter格式化UTC日期

我有以下日期:

2011-10-20T01:10:50Z
Run Code Online (Sandbox Code Playgroud)

我希望它被格式化为

"M/d/yy 'at' h:mma"
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    //The Z at the end of your string represents Zulu which is UTC
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-dd-MM'T'HH:mm:ss'Z'"];
    NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:@"created_at"]];

    //Add the following line to display the time in the local time zone
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"];
    NSString* finalTime = [dateFormatter stringFromDate:newTime];
    [dateFormatter release];
    NSLog(@"%@", finalTime);    
Run Code Online (Sandbox Code Playgroud)

不幸的是,finalTime在这里是NULL.

iphone cocoa-touch objective-c nsdate nsdateformatter

18
推荐指数
3
解决办法
2万
查看次数

如何在swift中格式化用户显示(社交网络)的时间间隔?

我有一个时间间隔,比如12600,相当于3小时30分钟.我如何格式化任何这样的时间间隔,以便只保留间隔的最高部分(例如在这种情况下图中,小时),并将正确的区域设置缩写附加到数字上.例如10米(10分钟),3d(3天),1年(1年).


编辑:以下是一些例子:

Time interval in: 90000    Whole string: 1d      String out: 1d
Time interval in: 900      Whole string: 15m     String out: 15m
Time interval in: 13500    Whole String: 3h 45m  String out: 4h

作为一般规则,应用正常的舍入规则(向下舍入3.4轮,向上舍入3.6轮).

nsdate ios swift

18
推荐指数
3
解决办法
9636
查看次数

来自NSDateComponents的NSDate

我试图将NSDate的hh,mm,ss组件改为另一个NSDate的组件 - 但出于某种原因,由于某种原因,时间设置为午夜.我无法弄清楚为什么!

这是代码;

+ (NSDate *) fixTime:(NSDate*)fromDate  toDate:(NSDate *) toDate {
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone localTimeZone]];
    [cal setLocale:[NSLocale currentLocale]];
    NSLog(@"fromDate=<%@>", [fromDate descriptionWithLocale:[NSLocale currentLocale]]);
    NSLog(@"toDate=  <%@>", [toDate descriptionWithLocale:[NSLocale currentLocale]]);

    NSDateComponents *fromTimeComponents = [cal components:( NSHourCalendarUnit   | 
                                                             NSMinuteCalendarUnit | 
                                                             NSSecondCalendarUnit   ) fromDate:fromDate];                               
    NSDateComponents *toDateComponents = [cal components:( NSYearCalendarUnit   | 
                                                           NSMonthCalendarUnit  |  
                                                           NSDayCalendarUnit      ) fromDate:toDate];
    NSDateComponents *toTimeComponents = [cal components:( NSHourCalendarUnit   | 
                                                           NSMinuteCalendarUnit | 
                                                           NSSecondCalendarUnit   ) fromDate:toDate];
    [toTimeComponents setHour:[fromTimeComponents hour]];
    [toTimeComponents setMinute:[fromTimeComponents minute]];
    [toTimeComponents setSecond:[fromTimeComponents second]];
    NSLog(@"toDateComponents year = …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsdate nsdatecomponents

17
推荐指数
1
解决办法
2万
查看次数

将格里高利日期字符串转换为伊斯兰日期会给出正确和错误的结果

我有以下两个日期字符串:(1)2013年 4 24日和(2)19/03/2013我正在尝试将这些日期转换为伊斯兰(Um Al Qura)日期,我正在使用此代码块来这样做:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"dd/MM/yyyy";
    df.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateInGrogrian = [df dateFromString:@"24/04/2013"];

    NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
    NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
    [df2 setCalendar:cal];
    [df2 setDateFormat:@"dd/MM/yyyy"];
    NSLog(@"Converted date to Islamic = %@",[df2 stringFromDate:dateInGrogrian]); 
Run Code Online (Sandbox Code Playgroud)

如果输入字符串是24/04/2013,则NSLog显示:

Converted date to Islamic = 14/06/1434这是正确的(根据所有伊斯兰国家使用的正式伊斯兰历法).但如果输入字符串是19/03/2013,则NSLog显示:

Converted date to Islamic = 08/05/1434 这是不正确的(根据伊斯兰历法,正确的日期必须是07/05/1434,这是1天后).

- 在您提出答案之前需要考虑的注意事项:

(1)我试图使用日历标识符NSIslamicCivilCalendar代替 …

calendar objective-c nsdate ios

17
推荐指数
2
解决办法
6614
查看次数

Swift转换不久前

我正在尝试不时转换,我想做的是:

从1到15秒它会说"刚才"

从60分钟到119分钟它会说"一小时前"

从24小时到47小时它会说"昨天"

从7天到7天23小时,它会说"一周前"

我不知道我的计数,如果我错了,请随时为我解决

这是代码

    extension NSDate {

    struct Date {
        static let timeFormatter: NSDateFormatter = {
            let df = NSDateFormatter()
            df.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
            df.dateFormat = "h:mm a"
            return df
            }()
    }
    var time: String { return Date.timeFormatter.stringFromDate(self) }

    var year:    Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Year,   fromDate: self) }
    var month:   Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Month,  fromDate: self) }
    var day:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Day,    fromDate: self) }
    var hour:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Hour,    fromDate: self) }
    var minute: …
Run Code Online (Sandbox Code Playgroud)

nsdate parse-platform swift

17
推荐指数
5
解决办法
1万
查看次数