I'm new to iOS app development. It will be great if someone helps me. I have a NSDateFormatter with the date format like M/d/yy hh:mm a and it displays correct result as
3/24/2016 12:00AM.
But i want to add String
at in between date and time. Expecting result as
3/24/2016 at 12:00AM
Your format could be something like
M/dd/yyyy' at 'hh:mm a
3/24/2016 at 12:00 AM
You can check this link for further details.
Format String Output String
M/d/y 11/4/2012
MM/dd/yy 11/04/12
MMM d, ''yy Nov 4, '12
MMMM November
E Sun
EEEE Sunday
'Week' w 'of 52' Week 45 of 52
'Day' D 'of 365' Day 309 of 365
QQQ Q4
QQQQ 4th quarter
m 'minutes past' h 9 minutes past 8
h:mm a 8:09 PM
HH:mm:ss's' 20:09:00s
HH:mm:ss:SS 20:09:00:00
h:mm a zz 8:09 PM CST
h:mm a zzzz 8:09 PM Central Standard Time
yyyy-MM-dd HH:mm:ss Z 2012-11-04 20:09:00 -0600
You can combine any of the options as per your requirements.
To do it properly, with localization in mind, you will have to localize at, too:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateStyle = NSDateFormatterNoStyle;
timeFormatter.timeStyle = NSDateFormatterShortStyle;
// load date format from localization files
NSString *dateFormat = #"%1$# at %2$#"; //NSLocalizedString(#"my_date_format", #"My date format");
NSDate *date = [NSDate date];
NSString *localizedDate = [NSString stringWithFormat:dateFormat, [dateFormatter stringFromDate:date], [timeFormatter stringFromDate:date]];
NSLog(#"%#", localizedDate);
Also note that I didn't hardcode date formats but I have used the ones defined by the user (language & region settings) instead.
You just need to M/d/yy' at 'hh:mm a as formatter.
Please use below code for swift Language.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "M/dd/yyyy"
var result: NSString = dateFormatter.stringFromDate(NSDate())
let timeFormatter = NSDateFormatter()
timeFormatter.dateFormat = "hh:mm a"
result = NSString(format: "%# at %#", result,timeFormatter.stringFromDate(NSDate()))
print(result)
Also, if you are doing development in Objective C then you can use below code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"M/dd/yyyy";
NSString *result = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:[NSDate date]]];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateFormat = #"hh:mm a";
result = [NSString stringWithFormat:#"%# at %#",result,[timeFormatter stringFromDate:[NSDate date]]];
NSLog(#"%#",result);
So replace
"MM/dd/yy hh:mm a"
fomatter by
"MM/dd/yy 'at' hh:mm a"
NSDateFormatter *dFormatter = [NSDateFormatter new];
[dFormatter setDateFormat:#"MM/dd/yy hh:mm a"];
NSDate *date = [dFormatter dateFromString:dateStr];
[dFormatter setDateFormat:#"MM/dd/yy 'at' hh:mm a"];
NSString *dateStr = [dFormatter stringFromDate:date];
Related
hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.
I am getting date from json in format "1/31/2016 5:53:21 AM" & i want to convert it to the format "31 Jan'16"...
I tried this code..
myDate=#"01/31/2016 05:53:21 AM";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yyyy hh:mm:ss tt";
NSDate *dtNew = [dateFormatter dateFromString:myDate];
dateFormatter.dateFormat = #"dd MMM'YY";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
but myDate string can't convert to NSDate. & dtNew comes nil.
Anyone can help?
Thanks in advance.!!!
try this
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
// "1/31/2016 5:53:21 AM" // initial date
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
NSDate *dtNew = [dateFormatter dateFromString:#"1/31/2016 5:53:21 AM"];
// 31 Jan'16 // second date
dateFormatter.dateFormat = #"dd MMM''yy";
NSLog(#"%#",[dateFormatter stringFromDate:dtNew]);
final output is
Try this, you should use format ending 'a' and double ' to escape it.
NSString *myDate=#"01/31/2016 05:53:21 AM";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *dtNew = [dateFormatter dateFromString:myDate];
dateFormatter.dateFormat = #"dd MMM''yy";
NSLog(#"%#",[dateFormatter stringFromDate:dtNew]);
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)
I write below code to convert one date format into another dateformat.
But AM string works and PM did not work .
Please help-
dateTime=#"2013-01-24 18:30 PM";
//dateTime=#"2013-01-24 06:30 AM";
NSDateFormatter *dtFormatter=[[NSDateFormatter alloc]init];
[dtFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate *AptDate=[dtFormatter dateFromString:dateTime];
[dtFormatter setDateFormat:#"MMM dd,yyyy hh:mm a"];
NSString *strDate=[dtFormatter stringFromDate:AptDate];
Here I get AptDate variable as null if i use PM format
You need to use HH in place of hh, as your time is in 24 hour format:
[dtFormatter setDateFormat:#"yyyy-MM-dd HH:mm a"];
Your full code will go as:
*dateTime=#"2013-01-24 18:30 PM";
//dateTime=#"2013-01-24 06:30 AM";
NSDateFormatter *dtFormatter=[[NSDateFormatter alloc]init];
[dtFormatter setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSDate *AptDate=[dtFormatter dateFromString:dateTime];
[dtFormatter setDateFormat:#"MMM dd,yyyy hh:mm a"];
NSString *strDate=[dtFormatter stringFromDate:AptDate];
NSLog(#"===> %#",AptDate);
NSLog(#"===> %#",strDate);
Output:
===> 2013-01-24 07:00:00 +0000
===> Jan 24,2013 12:30 PM
UILabel *dateLabel = (UILabel *)[cell viewWithTag:400];
NSString *oldDate = [exercise objectForKey:#"date"];
//make formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; //24 hour format
NSDate *tempDate=[formatter dateFromString:oldDate];
NSLog(#"temp date %#", tempDate);
[formatter setDateFormat:#"hh:mm a"];
NSString *newDate=[formatter stringFromDate:tempDate];
NSLog(#"new date %#", newDate);
//make label text
[dateLabel setText:newDate];
For anyone looking from mySQL with a DATETIME stamp to a regular hh:mm a format showing AM/PM correctly this worked for me.
For those who are looking for Swift 3 version of Accepted answer.
let date1 = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss a"
print(formatter.string(from: date1))
let dateTime = "2013-01-24 18:30 PM"
let dateFormater = DateFormatter()
dateFormater.dateFormat = "yyyy-MM-dd HH:mm a"
let AptDate = dateFormater.date(from: dateTime)
dateFormater.dateFormat = "MMM dd,yyyy hh:mm a"
let strDate = dateFormater.string(from: AptDate!)
print("===> \(AptDate!)")
print("===> \(strDate)")
hi i have problem while converting Date Formats...
the date time is in the Format 12 hour Format like 12/9/2010 4:00:00 PM
(Month/date/year Hour:Min:sec Pm)
i need to Convert it into 24 hour format like 12/9/2010 16:00:00
Can any one Help me please....
Thx in advance
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = #"hh:mm a";
NSDate *date = [dateformatter dateFromString:departTime];
dateformatter.dateFormat = #"HH:mm";
NSString *time24 = [dateformatter stringFromDate:date];
departTimelbl.text=time24;
Simply do This to get 24 hour time format (In Swift 2)
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm a" //(hh:mm a----> for 12 hour format)
let timeString = formatter.stringFromDate(NSDate())
print(timeString)
UPDATE: Swift 3 version
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm a" //(hh:mm a----> for 12 hour format)
let timeString = formatter.string(from: Date())
print(timeString)
You need to use a NSDateFormatter. I encourage you to read the class reference as linked by Kay.
Here's how you'd do it. I'm assuming the date is originating as a NSString.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
NSDate *myDate = [df dateFromString:yourNSString];
NSString *myNewDate = [formatter stringFromDate:myDate];
[formatter release];
[myDate release];
Use these functions,this will change the date string from 12 to 24 hr formate and 24 to 12hr formate.
-(NSString *)changeformate_string24hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
return [df stringFromDate:wakeTime];
}
-(NSString *)changeformate_string12hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
return [df stringFromDate:wakeTime];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm";
NSDate *date = [dateFormatter dateFromString:#"15:15"];
dateFormatter.dateFormat = #"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];
Take a look at NSDateFormatter class reference and the data formatting guide. There you will find more especially the method dateFromString and stringFromDate are interesting for you. There is another question dealing with similar problem From string with locale to date on iphone sdk where you can find some sample code when you want to set a special formatting template string.
In Swift
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"
var date: Date? = dateFormatter.date(from: "12/9/2010 4:00:00 PM")
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
var convertedString: String? = nil
if let aDate = date {
convertedString = dateFormatter.string(from: aDate)
}
In Objective C
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
NSDate *date = [dateFormatter dateFromString:#"12/9/2010 4:00:00 PM"];
dateFormatter.dateFormat = #"MM/dd/yyyy HH:mm:ss";
NSString *convertedString = [dateFormatter stringFromDate:date];