Format date and time using iPhone SDK?

How do I format the date and time using the iPhone SDK?

For example, how can I extract a date from 9/4/2024 12:00:00 AMin NSDate?

+5
source share
4 answers

you can format your string using dd / MM / yyyy hh: mm: ss a

example below:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"%@", [dateFormatter dateFromString:@"12/12/2012 12:12:12 AM"]);
+13
source

another option

Now you need all the string formats that you can use with NSDateFormatter. That's what


a: AM / PM

A: 0 ~ 86399999 (Millisecond of the day)

c / cc: 1 ~ 7 (Day of the week)

ccc: Sun / Mon / W / Wed / Th / Fri / Sat

cccc: sunday / monday / tuesday / wednesday / thursday / friday / saturday

d: 1 ~ 31 (0 complemented day of the month)

D: 1 ~ 366 (0 )

e: 1 ~ 7 (0 )

E ~ EEE: //////

EEEE: //////

F: 1 ~ 5 (0 , = )

g: ( 4713 .. 1 )

G ~ GGG: BC/AD ( )

GGGG: /Anno Domini

h: 1 ~ 12 (0 (12 ))

H: 0 ~ 23 (0 (24 ))

k: 1 ~ 24 (0 (24 )

K: 0 ~ 11 (0 (12 ))

L/LL: 1 ~ 12 (0 )

LLL: ///////////

LLLL: ///////////

m: 0 ~ 59 (0 )

M/MM: 1 ~ 12 (0 )

MMM: ///////////

MMMM: ///////////

q/qq: 1 ~ 4 (0 )

qqq: Q1/Q2/Q3/Q4

qqqq: 1- /2- /3- /4-

Q/QQ: 1 ~ 4 (0 )

QQQ: Q1/Q2/Q3/Q4

QQQQ: 1- /2- /3- /4-

s: 0 ~ 59 (0 )

S: ( -)

u: (0 padded Year)

v ~ vvv: ( )

vvvv: ( GMT)

w: 1 ~ 53 (0 , 1- = , NB: 1- )

W: 1 ~ 5 (0 , 1- = )

y/yyyy: ( )

yy/yyy: (2 )

Y/YYYY: ( , 1- )

YY/YYY: (2 , 1- )

z ~ zzz: ( GMT)

zzzz: ( GMT)

Z: +0000 ( RFC 822)

+48

Use this link for dateformat OR Check this code

Click this link here.

NSString *Datee=@"1/06/2013 8:11:12 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:Datee];
NSLog(@"%@", [dateFormatter dateFromString:]);
0
source

You can use the following method, just pass the date to it:

-(NSString *)getDateFromString:(NSString *)string
{

    NSString * dateString = [NSString stringWithFormat: @"%@",string];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"your current date format"];
    NSDate* myDate = [dateFormatter dateFromString:dateString];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"your desired format"];
    NSString *stringFromDate = [formatter stringFromDate:myDate];

    NSLog(@"%@", stringFromDate);
    return stringFromDate;
}
0
source

All Articles