NSDate time converting from 12hr to 24 hr format - ios

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];

Related

dateFromString returns nil. Unable to convert the date to the desired format

This is my input nsstring 30/01/18 3:25 PM
I want the date in this format : 30/Jan/18 3:25 PM.
This is what i tried :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"dd/MM/yy HH:mm "];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm "];
NSString *finalDate = [dateFormatter stringFromDate:date];
Also i tried this :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yy HH:mm z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm z"];
NSString *finalDate = [dateFormatter stringFromDate:date];
I am always getting date as nil !! What am i doing wrong?
Also : Time comes in both 12 hour format and 24 hour format. How do i handle both at the same time?
You check it below function, its working fine: -
-(void)DateChange
{
NSString *Input_Date =#"31/01/18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/mm/yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"dd/MMM/yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 31/Jan/18 04:25 AM
Use this function written in Swift to change the date format. Its working not
func dateFormatterfromString(_ date: String!) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
let date = dateFormatter.date(from:date)!
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter.string(from:date)
return dateString
}

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

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.

NSDateFormatter with custom string inbetween

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];

Format "1/31/2016 5:53:21 AM" to "31 Jan'16" in iOS

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]);

iOS - Init NSDate with a stringDate without timezone calculations

I am getting a date time from web service which is in UTC format. Now i want to assign it to NSDate. Following is the code i have done
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *utcDate = [dateFormatter dateFromString:#"2015-08-18T23:00:00"];
but it is calculating its timezone calculations by default the result is 2015-08-19 03:00:00 +0000. How can i initialize NSDate with same date and Time. I want to perform timezone calculations later on
edit/update:
Xcode 11 • Swift 5.1:
let dateString = "2015-08-18T23:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
if let dateFromString = formatter.date(from: dateString) {
print(dateFromString) // "2015-08-18 23:00:00 +0000"
}
A default-allocated NSDateFormatter will use the current locale (the one that the user set up in Settings.app).
You have to explicitly set a suitable locale on the formatter:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
This locale uses the gregorian calendar on UTC without any DST adjustments.
Edit: LeoDabus points out that setting the locale does not change the timezone of the date formatter. This has to be done explicitly:
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
NSString *dateStr =[NSString stringWithFormat:#"%#",[dict valueForKey:#"utc_datetime"]];
NSString* input = dateStr;
NSString* format = #"dd MMM yyyy - HH:mm:ss'";
// Set up an NSDateFormatter for UTC time zone
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
[formatterUtc setDateFormat:format];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Cast the input string to NSDate
NSDate* utcDate = [formatterUtc dateFromString:input];
// Set up an NSDateFormatter for the device's local time zone
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setDateFormat:format];
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]];
// Create local NSDate with time zone difference
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
NSLog(#"utc: %#", utcDate);
NSLog(#"local: %#", localDate);
NSDateFormatter *format1q = [[NSDateFormatter alloc] init];
format1q.dateFormat = #"HH:mm";
NSString *StrDAte=[NSString stringWithFormat:#"%#",[format1q stringFromDate:utcDate]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm";
NSDate *date = [dateFormatter dateFromString:StrDAte];
date = [date dateByAddingTimeInterval:-60];
dateFormatter.dateFormat = #"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];

Resources