How can I get the current date, and format it like "20/12/2010"?
Use NSDate and NSDateFormatter.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
See also.
Break it down.
How do I get the current date? See NSDate.
How do I format a date in a specific format? See NSDateFormatter.
Here is a method to retrieve the current date and print as a string using Swift on IOS 9
let formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "d/M/yy"
let myStr : String = formatter.string(from: NSDate.init(timeIntervalSinceNow: 0) as Date)
print(myStr)
XCode Debug Print
5/6/16
Related
I need to convert an NSString to an NSDate, but the following code only works as long as the user's device isn't set to 24-Hour time.
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *aTime = #"2/11/2013 12:00:00 AM";
NSDate *aDate = [formatter dateFromString:aTime];
However, aDate returns null when the device is in 24-Hour time. Any help?
I think you're probably seeing a side effect of the behaviour described by QA1480. Apple has resolved what it presumably thought was developers not obeying locales properly by modifying your prescribed date format unless you explicitly say not to.
You can achieve that by adding:
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
/* and autorelease if not using ARC */
Which basically says 'the date format I just set is explicitly what I want, please don't modify it in any way'.
I think the following snippet might helps you out.
NSString *dateString = #"09:34 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd-MM-yyyy";
NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];
dateFormatter.dateFormat = #"dd-MM-yyyy hh:mm a";
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDate *returnDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# %#", stringFromDate, dateString]];
try this code
NSString *string = #"2015-11-30 15:00:00";
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [formatter dateFromString:string];
date: 2015-11-29 21:30:00 +0000
its in UTC format you can modify this as you need!
Am working in an iPhone app with using the NSDateFormatter. Am getting the time from the webservice like "00:00:00", "16:00:00","15:30:00" and so on. I need to convert these time to
if the time is "00:00:00" to "12 AM", if the time is "16:00:00" to "4 PM", if the time is "15:30:00" to "3.30 PM". I have used the below code to this but when i have pass "16:00:00" the date returns null.
NSString *dats1 = #"16:00:00";
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:#"hh:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(#"date1 : %#", date1); **// Here returning (null)**
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
When am passing "00:00:00" it is returning "12 AM" correctly. Can anyone please help me to solve this issue?
If your time format is in 24hrs please use "HH:mm:ss" format. Please test the code and let me know if it is working for you. I have tested and it is returning "4 PM"
NSString *dats1 = #"16:00:00";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:#"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(#"date1 : %#", date1); **// Here returning (null)**
[dateFormatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
Thanks.
You can use the .ShortStyle which will adapt to the current locale:
let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())
Which returns 17:12 or 5:12 PM depending on the current device locale.
Why are you again allocating the NSDateFormatter ?
you Just need to format it to NSDate and then again to NSString,
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dats1];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"%#",formattedDate);
Try HH:mm:ss (notice the capitals in the hour format)
Hope someone would be helpful who would like to get it in Swift
Swift 5.1+
let dateFormatter = DateFormatter()
//if you want 12AM,5PM then use "hh mm a"
//if you want 00AM,17PM then use "HH mm a"
dateFormatter.dateFormat = "hh mm a"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
This Below code always keep in 12hour format:
NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
df.locale = 12hourFomrat;
This always keep in 24 hour format
NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
df.locale = 24hourFormat
your date format should be:
[dateFormatter3 setDateFormat:#"HH:mm:ss"];
check the Date Format Pattern Doku
Swift 5:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh mm a"
let formattedDate = dateFormatter.string(from: Date())
I am getting date in string formate ad "2012-04-11T07:51:10+0000" & I want to convert it to NSDate as "11-04-2012". I dont understand how to use "setDateFormat" for this.
This is what I am doing--
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'zzzz"];
Please help.
And whatever NSDate I will get I again want to convert to string ie. only 11-04-2012.
I have searched on ggl. But no satisfactory result :(
please help.
Here is how to parse the string to generate a date:
NSString *dateString = #"2012-04-11T07:51:10+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyy-MM-dd'T'HH:mm:ssZ";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
Now you can generate a new string representation of the date as follows:
formatter.dateFormat = #"dd-MM-yyyy";
NSString *newDateString = [formatter stringFromDate:date];
NSLog(#"New string: %#", newDateString);
// 11-04-2012
The problem with the dateFormat you were using is that you don't have to escape all the symbols such as 'MM' and that zzzz is not the correct symbol to parse the timezone of type +0000.
Have a look at this documentation for more information.
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100"; // change value base on your requirement.
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"]; // Change format base on dareStr.
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"]; // Change format base on your output requirement
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];
I have in variable value from PHP function mktime(time from epoch), exacly 133123088.
How I can change it for readable date in iphone?
Is any function to convert this format to another like NSDate or just string ?
NSDate* date = [NSDate dateWithTimeIntervalSince1970: 133123088]
NSTimeInterval timeInterval = 133123088; // NSTimeInterval is a double
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
Notice that 133123088 corresponds to a date in 1974. So I guess you are missing a digit in 133123088 to make it a date in 2012: For example : 1331230880
NSTimeInterval timeInterval = 1331230880;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy/MM/dd HH:mm:ss";
NSString *dateString = [formatter stringFromDate:date];
I have a string in this format "dd.mm.yy" (23.08.10).
I would like to convert them to a Date format using
NSDate *currentDate = [[NSDate alloc] initWithString:new.date];
But i need a format like this: "yyyy-mm-dd HH:MM:ss +0000" to compare two dates!
How can I do that? I know that I have to user NSDateFormatter but I dont know how to use it.
Thanks
NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"dd.MM.yy"];
NSDate *date = [dateformatter dateFromString:dateString];
[dateformatter release];
Have a look at the date markup.
Date_Format_Patterns