I'm new in iOS.
How to change date-picker display format ?
My requirement is like below image.
Give idea or suggestion
Thanks
In Apple's documentation they specified that in UIDatePickerModeDate. The DatePicker displays months, days of the month, and years. This order items depends on your device local setting. you can check this documentation Date Picker Description
if you want to same date picker then you could create your own picker.
It's not possible with native UIDatePicker you need to use the custom one for e.g UIPickerview
Try this :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:"pass your string date"];
[dateFormatter setDateFormat:#"EE, dd MMM, yyyy hh:mm a"]; // change format as per your needs
NSString * strTimestamp = [dateFormatter stringFromDate:date];
NSLog(#"your stirng%#",strTimestamp);
let dateFormatter = DateFormatter()
let todayDate:NSDate = NSDate()
dateFormatter.dateFormat = "dd MMM yyyy"
let str:String = dateFormatter.string(from: todayDate as Date)
print("String is ----%#",str)
You can write as this Sender date of your date picker
//let str: String = dateFormatter.string(from: sender.date)
Out put
String is ----%# 24 Apr 2017
Related
I want to take a string and turn it into a date but I cannot find the format how to enter the text so it is recognized as date. Please see the screenshot.
asking for the date as text (this is just to try it, as actually I get the date from a text property)
Convert it to date
Output as formatted date -> date conversion error
An answer can be found here.
They key is to not use the magic variable directly in "Use date", but select "Given date" and then use the magic variable for that. This way a format can be specified.
Use NSDate formatter to go from string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:[itemOnServerDict objectForKey:#"lastTimeSeen"]];
let dateFormatter = DateFormatter()
let enUSLocale = Locale(identifier: "en_US")
dateFormatter.locale = enUSLocale
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
let dateString = dateFormatter.string(from: Date())
You have to set the date format as the text is so
let dateFormatter = DateFormatter()
//here you have to pass the format of the date in the text
dateFormatter.dateFormat = "dd/MM/yy"
let date = dateFormatter.date(from: string)
then with this date you could parse to new string with new format like this
//set the desired format to the date
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter.strin(from: date)
This question already has an answer here:
Converting NSString to NSDate - wrong format
(1 answer)
Closed 4 years ago.
How do i convert string to date in Objective C.
I have tried the following but did not figure it out.
NSString *str = #"3/2/2018 11:44:32 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/d/yyyy h:mm:ss a"];
NSDate *resultDate = [[NSDate alloc] init];
resultDate = [df dateFromString:str];
NSLog(#"result date: %#", resultDate);
result date: 2018-03-02 06:14:32 +0000 ,but i need to get it as
result date: 3/2/2018 11:44:32 AM
You are correctly parsing the string to the Date object. The way it is presented by the print is because by default if printing an object, its description is printed. In case of Date, it will be always the format you get. But the date is correct.
If you want to get it presented the way it was before, again use the same dateFormatter and just format the date to string back:
NSLog(#"result date: %#", [df stringFromDate:resultDate]);
UPDATE
If the problem is the hour shift, that's due to your current timezone that will be used when parsing using DateFormatter. To overcome this, set explicitly timezone and locale of the date formatter, see this example (swift version, but you need just those two line with setting timeZone and locale on dateFormatter):
let dateString = "3/2/2018 11:44:32 AM"
let df = DateFormatter()
df.dateFormat = "MM/d/yyyy h:mm:ss a"
// set the timezone and locale of the dateformatter:
df.timeZone = TimeZone(identifier: "GMT")
df.locale = Locale(identifier: "en_US_POSIX")
let date = df.date(from: dateString)
// now it will print as you expect:
print(date)
Your issue is related to your app current locale, if your current locale is "en_US" then your NSLog(#"result date: %#", resultDate); line will print
result date: Fri Mar 2 11:44:32 2018
Maybe, this code will help you, again that date its convert to Nsstring, check it this code
-(void)DateChange
{
NSString *Input_Date =#"3/22/2018 11:44:32 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"M/d/yyyy hh:mm:ss a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"M/d/yyyy hh:mm:ss a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 3/22/2018 11:44:32 AM
I am getting a response from a server and there is a date string I need to convert into a date:
Thu Jun 29 07:15:25 +0000 2017
I am trying to convert the string into a human readable format. Can anyone suggest how to convert this string into date?
You need to parse the date string into a Date object using a DateFormatter, then create a string from that Date using another DateFormatter with the output format you want to use.
let created_at = "Thu Jun 29 07:15:25 +0000 2017"
let df = DateFormatter()
df.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
guard let date = df.date(from: created_at) else {return}
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = "EEE, MMM d"
let outputDate = outputFormatter.string(from: date)
If you want to know what date formats look like, use NSDateFormatter.com, which has an interactive interface where you can play around with different date formats and have examples of most common formats as well.
This is how you can format it in "Thu Jun 29"
-(NSString *)formatCreatedAt:(NSString *)createdAt
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:createdAt];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd"];
NSString *stringFromDate = [formatter stringFromDate:date];
return stringFromDate;
}
Pass the value of created_at in this method and the output will be returned in your specified format.
The string I get from server is in the format like "2015-11-09 06:54:00 UTC" I want to convert
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate* date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"dd MMM yyyy hh:mm a"];
NSString* formattedDate = [dateFormatter stringFromDate:date];
I tried with date formatter to be dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
Still I get the formatted date as nil.
I don't understand how to convert this string to a proper NSDate. Please suggest.
Try the below code snippet;
NSString *dateString = #"2015-11-09 06:54:00 UTC";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z";
NSDate* date = [dateFormatter dateFromString:dateString];
dateFormatter setDateFormat:#"dd MMM yyyy hh:mm a"];
NSString* formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"Formatted Date: %#",formattedDate);
I could say "try the code below", but that is useless.
Look at your formatting string. It says what the date formatter expects in the string.
Look at your date string.
Compare them. They don't match. The 'T' in the formatting string isn't some kind of black magic. It means "I expect a letter T in the date string". There is no letter T in the date string, that's why it doesn't work.
Read Apple's documentation carefully, and take note of the bit where it refers to the Unicode standard. And read that document at www.unicode.org carefully - There is a HUGE difference between yyyy and YYYY, and between MM and MMM, and between HH and hh, and if you don't read this and learn this, you cannot write code that gives the desired result.
I am trying to format a date i am getting from twitter using the STTwitter library.
However the code that I've tried so far has not worked.
Code for getting the date from twitter:
NSString *dateString = [status valueForKey:#"created_at"];
This returns the time, date, time zone and year in which the tweet was made which looks messy.
I tried using the following code to convert this and make it neater:
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMddHHmm"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#", dateFromString);
dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd MMMM' at 'hhmm a"];
NSString *mydate=[dateFormatter stringFromDate:dateFromString];
And then try to put the result in a text label:
cell.detailTextLabel.text = my date;
Ive tried many different variations of the Date Formatter but none have worked and i have no idea why.
Thanks for your help :)
The date format you are using is not even close the date string used in the result, which is something like Fri Nov 18 20:35:49 +0000 2011.
NSString *dateStr = #"Fri Nov 18 20:35:49 +0000 2011";
NSDateFormatter *dateFormatter= [NSDateFormatter new];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The real trick is in the locale used, since the date is localized in english.
STTwitter has a category for that:
NSString *s = [tweet valueForKey:#"created_at"];
NSDate *date = [[NSDateFormatter stTwitterDateFormatter] dateFromString:s];
I created a gist with Swift implementation of it:
https://gist.github.com/appzzman/62339fcd10bbe8fce256 It takes Twitter date and lets you specify the output format of the date.
import UIKit
func parseTwitterDate(twitterDate:String, outputDateFormat:String)->String?{
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
var indate = formatter.dateFromString(twitterDate)
var outputFormatter = NSDateFormatter()
outputFormatter.dateFormat = "hh:mm a dd:MM:yy"
var outputDate:String?
if let d = indate {
outputDate = outputFormatter.stringFromDate(d)
}
return outputDate;
}
var str = "Wed Sep 02 19:38:03 +0000 2009"
var outputDateFormat = "hh:mm a dd:MM:yy"
parseTwitterDate(str, outputDateFormat)