-3

I have two different values of timestamp on same day in different time. So I want to provide format 'yymmdd' to the NSDate to covert date into timestamp since the value changes acc. to time. If anyone could help me in this or suggest me since i am new to Swift.

Ranjana Dangol
  • 1,319
  • 2
  • 13
  • 24
  • [`NSDateFormatter`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/)!? – luk2302 Sep 11 '15 at 12:41
  • 1
    Your Objective-C code does *not* create a timestamp with YYYYMMDD format. It simply creates a string representing the seconds since 1 Jan, 1970. – Martin R Sep 11 '15 at 12:46
  • 1
    1441975738932.99 this is the o/p of your code. This is definatly not YYYYMMDD format – iAnurag Sep 11 '15 at 12:53
  • 1
    I dont know if it displayed the value i wanted .. I found the code someone replied to similar type of question. I am new to this programming field and confused about obj c.. Could u please answer the edited question above. – Ranjana Dangol Sep 11 '15 at 15:43
  • 1
    @luk2302 I used NSDateFormatter to change the format .. But again i need the value in timestamp which results as human readable date value. – Ranjana Dangol Sep 11 '15 at 15:44
  • this make sense for you? http://stackoverflow.com/questions/22359090/get-current-time-in-timestamp-format-ios There is Obj-C and Swift examples – ゴスエン ヘンリ Sep 11 '15 at 15:46
  • @RanjanaDangol I am struggling to understand what do you need. Can you give an example? Do you need to convert an NSDate into a NSString? – tanz Sep 11 '15 at 15:46
  • 1
    @tanzolone I want to convert NSdate providing the format yymmdd to get the timestamp value... can u recheck the question which i have edited. – Ranjana Dangol Sep 11 '15 at 15:54
  • 1
    @ゴスエンヘンリ I have already gone through it but that is not what i wanted. The timestamp value changes at today 10pm and today 6 pm which avoids me to compare these values. So for date only 2015-09-11 i need timestamp – Ranjana Dangol Sep 11 '15 at 15:56
  • @RanjanaDangol what do you mean with timestamp? Can you make an example of what do you expect to see? – tanz Sep 11 '15 at 15:58
  • @tanzolone timestampt means the epoch unix timestamp – Ranjana Dangol Sep 14 '15 at 05:00
  • @RanjanaDangol I wrote you an answer on how to retrieve the epoch timestamp. I would suggest you to edit your question as it is not clear that you're looking for the unix timestamp just reading the question. Thanks. – tanz Sep 15 '15 at 08:45

1 Answers1

0

I was a bit confused about what you were asking for but after reading your own comments/answers to the original post I understand that you are looking for "the epoch unix timestamp". You can easily get this from any NSDate object as follows:

NSDate *yourDate = [NSDate date];//now in this example

NSTimeInterval epochTimestamp = [yourDate timeIntervalSince1970];

NSString *epochTimestampString = [@(epochTimestamp) stringValue];

NSLog(@"%@",epochTimestampString);

This way you will get the epoch timestamp defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

tanz
  • 2,557
  • 20
  • 31