0

I have the date and time , what i want is the no of milliseconds from the particular date and time

for eg: date is 21-04-2015 and time on the date(21-04-2015) is 14:23

then the output in milliseconds is 1429606437692 I want the no of milliseconds from 1970 to todays date up to 14:23 time how to get this date time in millisecond in iOS?

please help and thanks in advance.

Krunal
  • 6,440
  • 21
  • 91
  • 155

4 Answers4

6

Following code for getting date and time into Millisecond.

NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970];

For more detail refer apple documentation.

----Edited----

NSString *dateString = @"21-04-2015 14:23";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSTimeInterval timeInMiliseconds = [dateFromString timeIntervalSince1970]*1000;
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
1

For that you need to multiply time gap by 1000, as time interval is in seconds and as float value. so simply multiply by 1000. For simple example I add selector

 NSDate *lastOneminute = [NSDate date];
[self performSelector:@selector(timeInterval:) withObject:lastOneminute afterDelay:60.0];

and after 60 seconds selector call is :

 - (void)timeInterval:(id)timeGap
{
    NSLog(@"time interval : %@",timeGap);

    NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSinceDate:timeGap] * 1000;
    NSLog(@"milisecond : %f",timeInMiliseconds);
}
Kamal Bhardwaj
  • 948
  • 1
  • 10
  • 25
1
double timestamp = [[NSDate date] timeIntervalSince1970];
int64_t timeInMilisInt64 = (int64_t)(timestamp*1000);

#pragma mark - Date


   -(void)DateCalc
    {


        NSDate *today = [NSDate date];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        dateString = [dateFormat stringFromDate:today];


        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *comps = [NSDateComponents new];
        comps.day = 7;
        NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        EndingDate = [dateFormat stringFromDate:sevenDays];


    }
SARATH SASI
  • 1,395
  • 1
  • 15
  • 39
0

You can use below code

    NSString *dateStr = @"21-04-2015 14:23";

    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-YYYY HH:mm"];
    NSDate *date = [dateFormat dateFromString:dateStr];

    ////2014-12-21 08:53:00 +0000 date
    //// 1419151980000 -- milli seconds    
    NSTimeInterval timeInMiliseconds = [date timeIntervalSince1970]*1000;

Hope it helps you...!

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59