Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to timestamp in iOS

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

like image 520
user2799156 Avatar asked Jan 15 '14 06:01

user2799156


People also ask

What are the different types of timestamps used in date converter?

The converter on this page converts timestamps in seconds (10-digit), milliseconds (13-digit) and microseconds (16-digit) to readable dates. How to get the current epoch time in ...

Is timestamp in milliseconds or seconds?

These examples are returning timestamp in seconds, although some of the languages are returning timestamp in milliseconds. These examples are showing how to get current date and time that could be presented to the end-user. These examples are showing how to convert timestamp - either in milliseconds or seconds to human readable form.

What is a core data timestamp?

A Core Data timestamp is the number of seconds (or nanoseconds) since midnight, January 1, 2001, GMT (see CFAbsoluteTime ). The difference between a Core Data timestamp and a Unix timestamp (seconds since 1/1/1970) is 978307200 seconds. The current Core Data timestamp is 682616486 or in nanoseconds: 682616486000000000

How do I convert Unix time to human-readable time format?

In Shortcuts, you can convert from the UNIX time format to a more human-readable time format by using the Date, Adjust Date, and Format Date actions. The Date action is set to 00:00:00 UTC Jan 1, 1970. Add the UNIX Time variable to the UTC date to get the adjusted date. Next, use Format Date to remove the time component (as they are all 00:00:00).


2 Answers

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

  • Convert Date to Integer in Swift
  • Creating a Date and Time in Swift
  • How to get the current time as datetime
like image 58
Suragch Avatar answered Oct 06 '22 09:10

Suragch


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]) ;
like image 20
KudoCC Avatar answered Oct 06 '22 08:10

KudoCC