nsdateformatter

Shared NSDateFormatter - Best Practices?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 19:55:05
问题 My team found that we were using a variety of NSDateFormatter objects throughout our code base, and started looking into how we could avoid the cost/confusion of allocating/initializing common formatters in a bunch of separate places. One idea we had was to create a category on the NSDateFormatter class that would provide a reference to a static instance of a commonly configured formatter. For example, we were using the "short time" date formatter in several places, and were looking to add

Date from String using NSDateFormatter regardless 12h-24h setting

回眸只為那壹抹淺笑 提交于 2019-12-05 19:47:43
Today my question is about date formats and strings. My application downloads some strings representing dates from the internet. The date format is always like this: "2010-05-24 at 20:45" I need to convert this string into an NSDate object in order to perform some date manipulations. I tried this code: NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init]; [myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"]; NSDate * dateFromString = [myDateFormatter dateFromString:dateString]; This seems

Swift 3 changing date format

空扰寡人 提交于 2019-12-05 19:35:53
I want to get my Date in DD.MM.YYYY HH:mm:ss as a String. I use the following extension: extension Date { var localTime: String { return description(with: Locale.current) } } and the following code when my datePicker changes: @IBAction func datePickerChanged(_ sender: UIDatePicker) { dateLabel.text = datePicker.date.localTime let formatter = DateFormatter() formatter.dateFormat = "dd.MM.yyyy hh:mm" let TestDateTime = formatter.date(from: datePicker.date.localTime) } What am I doing wrong? Your code is completely wrong. Just do the following: @IBAction func datePickerChanged(_ sender:

Date formatter returns nil for June

会有一股神秘感。 提交于 2019-12-05 18:58:02
问题 I have a strange problem with those lines of code : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"062008"]; NSLog(@"Date %@", date); The result is : Date (null) But when I change the month like this : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"072008"]; NSLog(@"Date %@", date); The result is : Date 2008-06-30 23:00:00 +0000 For only the

GMT and UTC formatter with NSDateFormatter

☆樱花仙子☆ 提交于 2019-12-05 18:52:47
i wrote function that converting current time utc-gmt or gmt -utc. The functions just works fine if msgArrivedDate is null. If it's not ( that means , msgArrivedDate comes from rest service that doses not convert . jSON parse part : NSArray *messageSentTime = [[args valueForKey:@"messageSendDate"] objectAtIndex:0]; for(int i=0 ;i< [messageSentTime count]; i++) { //[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]; NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]); Function part : -(id)timeZoneFormatter:(NSString *)formatType :

NSDate/NSDateFormatter - Storing only time, not date?

有些话、适合烂在心里 提交于 2019-12-05 18:14:21
问题 I've been looking around but I haven't seen anything that addresses this so I'm hoping someone can help clear this up for me. What I am trying to do is use an NSDate variable(in core data) to store a time, not date and time, but just time in the format HH:MM:SS; After looking at the NSDateFormatter class reference and the sample code provided I was able to tweak it and think that my code should look something like the following: NSDateFormatter *timeOfArrival = [[NSDateFormatter alloc] init];

NSDate to RFC 2822 Date format

懵懂的女人 提交于 2019-12-05 16:54:29
Is there any efficient way to convert an NSDate to RFC 2822 Date format string ? I want to use this string to create an NSURLRequest and set the value of "If-Modified-Since" header field. try to use an NSDateFormatter NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // maybe there exist a new-method now dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format NSString *dateString = [dateFormatter stringFromDate:myDate]; Swift 3 let rfcDateFormat = DateFormatter() rfcDateFormat.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z" let dateSTring = rfcDateFormat.string

Parse JSON date into NSDate depending user settings

隐身守侯 提交于 2019-12-05 10:00:47
问题 I have a problem for converting a brut JSON string date: "created_at" = "2012-12-22T21:39:22Z"; into an NSDate. Here is my current category for that: - (NSDate*)dateWithJSONString { [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [dateFormatter setCalendar:[[NSCalendar alloc]

How can I format two NSDate as a time difference string (e.g. 2days ago) and make it follow the regional formatting of the device?

无人久伴 提交于 2019-12-05 04:55:31
问题 Does the standard framework support time difference formatting and create a format that follows the regional settings? I know I can break it to NSDateComponents but then I will have to append the text and create different language support files myself. I'm wondering that there may be a way to formatting the date and make it follows the regional setting simple and similar to this... dateFormat = [[[NSDateFormatter alloc] init] autorelease]; [dateFormat setDateStyle:NSDateFormatterMediumStyle]

how to get start and end time of today's date in ios? [duplicate]

一笑奈何 提交于 2019-12-05 03:34:14
This question already has answers here : NSDate beginning of day and end of day (21 answers) Closed 3 years ago . I am getting current date and time by using this code let today: NSDate = NSDate() let dateFormatter: NSDateFormatter = NSDateFormatter() dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" dateFormatter.timeZone = NSTimeZone(abbreviation: "SGT"); print(dateFormatter.stringFromDate(today)) but i want to get start time and end time of today’s date example : 12-09-2016 00:00:00 AND 12-09-2016 23:59:59 How to get start and end