nsdateformatter

How to convert NSDate in to relative format as “Today”,“Yesterday”,“a week ago”,“a month ago”,“a year ago”?

馋奶兔 提交于 2019-12-27 19:09:22
问题 I want to convert nsdate in to relative format like "Today","Yesterday","a week ago","a month ago","a year ago","date as it is" . I have written following method for it.. but some how its just printing as it is date.. can you please tell me what should be the problem? //Following is my function which converts the date into relative string +(NSString *)getDateDiffrence:(NSDate *)strDate{ NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.timeStyle = NSDateFormatterMediumStyle; df

Error while converting C# date string into swift3 date

南楼画角 提交于 2019-12-25 19:09:08
问题 In one of my app i am getting date from an RESTful api as "04112017182149". I tried to convert it into swift date for my internal user as shown in below snippet. let receivedDate = "04112017182149" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" print((dateFormatter.date(from: receivedDate))!) It is throwing a fatal error as Unexpectedly found nil while unwrapping optional value. I had tried to change date formatter string as "dd-MMM-yyyy HH:mm:ss" but no use. 回答1:

Convert date format from json data in the model and then pass to controller

旧城冷巷雨未停 提交于 2019-12-25 19:06:14
问题 Here in my model I have a few struct to handle the data from server struct Articles: Decodable { var content : [ArticleData] } struct ArticleData: Decodable { var title: String var date_time: Date var image: String } From the jason, I get a date, it's like that: "date_time": 1524842056.035, First question: I set the type of this type in model Date as you see in ArticleData , it that correct? Then I read this json in the model something like that: guard let json = try? JSONDecoder().decode

swift 3 - can not convert string to date [duplicate]

心不动则不痛 提交于 2019-12-25 18:45:13
问题 This question already has an answer here : dateFromString returns nil for some values (1 answer) Closed 2 years ago . No matter what I do, I can not convert an RSSs pubDate (which is a string of course) into a Date . Here is the date String: Sun, 02 Apr 2017 19:31:18 GMT Here is my code: private var dateBuffer: Date { formatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss zzz" let date = formatter.date(from: buffers["pubDate"]!) //<-date comes back nil return date! //<-program crashes } date

swift 3 - can not convert string to date [duplicate]

旧城冷巷雨未停 提交于 2019-12-25 18:45:07
问题 This question already has an answer here : dateFromString returns nil for some values (1 answer) Closed 2 years ago . No matter what I do, I can not convert an RSSs pubDate (which is a string of course) into a Date . Here is the date String: Sun, 02 Apr 2017 19:31:18 GMT Here is my code: private var dateBuffer: Date { formatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss zzz" let date = formatter.date(from: buffers["pubDate"]!) //<-date comes back nil return date! //<-program crashes } date

Issues with stringWithFormat

风流意气都作罢 提交于 2019-12-25 18:44:18
问题 Please how do I make my stringWithFormat/initWithFormat to display am/pm instead of 24hrs, even though users selects am/pm from the pickerviewer? When a user select time; eg 4:15 pm, instead of it to display 4:15pm as confirmation, it displays 16:15 instead. I want it as 4:15pm. PLEASE HELP!! -(IBAction)datePickerValueChanged:(id)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

How to convert a date string like “2011-06-25T11:00:26+01:00” to a long like iphone?

僤鯓⒐⒋嵵緔 提交于 2019-12-25 17:04:52
问题 I need to convert this string date "2011-06-25T11:00:26+01:00” into a long like value. I tried this NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; [df setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [df setDateFormat:@"yyyy-MM-ddHH:mm:ssZ"]; NSDate *date = [df dateFromString:[time stringByReplacingOccurrencesOfString:@"T" withString:@""]]; [df setDateFormat:@"eee MMM dd, yyyy hh:mm"]; NSLog(@"time%@", time); long lgTime = (long)[date timeIntervalSince1970]; but this

Need to convert custom timestamp to different format in Objective-C iOS

情到浓时终转凉″ 提交于 2019-12-25 17:04:42
问题 I have a string "2012-06-04" and am having a hard time converting it to: June 4, 2012 . Is there a quick way to transform this? I come from a ruby world where you would convert everything to seconds and that back out to the format you need it. Is there a reference that shows how to do that? Thanks 回答1: One way to do so is to convert the string to an NSDate using the NSDateFormatter with the 2012-06-04 format, and then convert the NSDate back to a string using the June 4, 2012 format: NSString

Getting wrong time difference

荒凉一梦 提交于 2019-12-25 16:58:30
问题 I not understand where i'm wrong to getting correct difference of two dates, -(void)updateTimeLabel{ NSString *serverResponseTimeString = @"02-08-2014 05:26:32"; NSString *systemDateString = @"02-08-2014 05:31:46"; NSDate *serverTime = [self dateFromWebStringResponse:serverResponseTimeString]; NSDate *systemDate = [self dateFromWebStringResponse:systemDateString]; NSLog(@"serverResponseTime %@", serverResponseTimeString); NSLog(@"serverTime %@", serverTime); NSLog(@"systemDate %@", systemDate

comparing time without date swift3

ε祈祈猫儿з 提交于 2019-12-25 14:40:31
问题 I am using an API to get a list of times which is five times a day as string in (AM,PM) format without date, ("4:30 AM","1:00 PM","3:20: PM","6:40 PM","9:10 PM") what I am trying to do is to show the next time in the screen depends on the current time on the iPhone so if the time now is 5:00 PM the time should display on the screen is 6:40 PM how can I compare times in swift without including the dates because I tried to convert the list of times I have to Date type but unfortunately am