6

How do you convert any given date to milliseconds? For example, 2014-01-23 to timestamp conversion.

NSDate *date = [dateFormatter dateFromString:@"2014-01-23"];
NSLog(@"date=%@",date);
NSTimeInterval interval  = [date timeIntervalSince1970];
NSLog(@"interval=%f",interval);
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval];
[dateFormatter setDateFormat:@"yyyy/mm/dd "];
NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]);

Output result: 1970/30/01

Suragch
  • 428,106
  • 278
  • 1,284
  • 1,317
user2799156
  • 357
  • 2
  • 3
  • 10

5 Answers5

22

Swift

Convert the current date/time to a timestamp:

// current date and time
let someDate = Date()

// time interval since 1970
let myTimeStamp = someDate.timeIntervalSince1970

See also

Community
  • 1
  • 1
Suragch
  • 428,106
  • 278
  • 1,284
  • 1,317
6

Have it a try. "mm" stands for minute while "MM" stands for month.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"yyyy-MM-dd"] ;
NSDate *date = [dateFormatter dateFromString:@"2014-01-23"] ;
NSLog(@"date=%@",date) ;
NSTimeInterval interval  = [date timeIntervalSince1970] ;
NSLog(@"interval=%f",interval) ;
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval] ;
[dateFormatter setDateFormat:@"yyyy/MM/dd "] ;
NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]) ;
KudoCC
  • 6,852
  • 1
  • 22
  • 50
  • hey i'm struggling with the time like i have a particular date and a time for example 1-january-2018 9:00 am how to get the timestamp of the particular date with particular time... – tryKuldeepTanwar Aug 01 '17 at 02:41
0

NSTimeinterval is really a double, being seconds since a particular date (in your case, the start of 1970)

Alex Brown
  • 40,336
  • 10
  • 90
  • 107
0

[date1 timeIntervalSinceDate:date2] will return seconds from two NSDate objects.

double seconds = [date1 timeIntervalSinceDate:date2];
double milliSecondsPartOfCurrentSecond = seconds - [seconds intValue];

milliSecondsPartOfCurrentSecond

Pushkraj P. Lanjekar
  • 2,183
  • 1
  • 20
  • 33
Zeeshan
  • 4,086
  • 26
  • 32
0

NOTE :- UNIX Timestamp format contains 13 Digits so we need a 1000 multiplication with the result. And the timestamp must be UTC ZONE not our local Time Zone.

- (void)GetCurrentTimeStamp
    {
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyy-MM-dd"];
        NSString    *strUTCTime = [self GetUTCDateTimeFromLocalTime:@"2014-01-23"];
        NSDate *objUTCDate  = [objDateformat dateFromString:strUTCTime];
        long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);
        NSLog(@"Local Time = %@---- UTC = %@ ----- TimeSatmp = %ld  ---- TimeStamp = %lld",strTime,strUTCTime,unixTime,milliseconds);
    }

 - (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate  *objDate    = [dateFormatter dateFromString:IN_strLocalTime];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSString *strDateTime   = [dateFormatter stringFromDate:objDate];
        return strDateTime;
    }
Vicky
  • 1,065
  • 16
  • 15