Current Date & Time in Objective C
In this tutorial , i will tell you about NSDate and NSDateFormatters .
NSDate is inbuilt Objective c function which is used to find current date and time .
NSDateFormatter is used to format the date and time .eg :- MM/DD/YYYY hh:mm:ss.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; // allocate and initialize NSDateFormatter | |
[dateFormatter setDateFormat:@"h:mm a MMM d, yyyy"]; // set the type of date and time format. | |
NSString *date = [dateFormatter stringFromDate:[NSDate date]]; // take NSString which will contain the date & time | |
// [NSDate date] is used to get the current date. | |
NSLog(@"Current date is %@",date). |
Code is explained in comments.
Other date formats are as follows :
Date Formats Examples
M/d/y | 23/7/1993 |
MM/dd/yy | 23/07/93 |
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 C |
h:mm a zzzz | 8:09 PM Central Standard Time |
yyyy-MM-dd HH:mm:ss Z | 2012-11-04 20:09:00 -0600 |
Thank You :)
Comments
Post a Comment