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 P
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];
}
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.
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)
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];
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];
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];