iOS NSDate

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • NSDate() // NSDate object init to the current date and time
  • NSDate().timeIntervalSince1970 // Current date and time in number of seconds from 00:00:00 UTC on 1 January 1970.
  • NSDate().compare(other: NSDate) // Returns a comparison of current date to another date returns a NSComparisonResult

Remarks

There are different types of date format that you can set: Here is full list of them.

FormatMeaning/DescriptionExample1Example2
yA year with at least 1 digit.175 AD → “175”2016 AD → “2016”
yyA year with exactly 2 digits.5 AD → “05”2016 AD → “16”
yyyA year with at least 3 digits.5 AD → “005”2016 AD → “2016”
yyyyA year with at least 4 digits.5 AD → “0005”2016 AD → “2016”
MA month with at least 1 digit.July → “7”"November" → "11"
MMA month with at least 2 digits.July → “07”"November" → "11"
MMMThree letter month abbreviation.July → “Jul”"November" → "Nov"
MMMMFull name of month.July → “July”"November" → "November"
MMMMMOne letter month abbreviation(Jan,June,July all will have 'J').July → “J”"November" → "N"
dDay with at least one digit.8 → “8”29 → “29”
ddDay with at least two digits.8 → “08”29 → “29”
“E”, “EE”, or”EEE”3 letter day abbreviation of day name.Monday → “Mon”Thursday → “Thu”
EEEEFull Day name.Monday → “Monday”Thursday → “Thursday”
EEEEE1 letter day abbreviation of day name.(Thu and Tue will be 'T')Monday → “M”Thursday → “T”
EEEEEE2 letter day abbreviation of day name.Monday → “Mo”Thursday → “Th”
aPeriod of day (AM/PM).10 PM → “PM”2 AM → “AM”
hA 1-12 based hour with at least 1 digit.10 PM → “10”2 AM → “2”
hhA 1-12 based hour with at least 2 digits.10 PM → “10”2 AM → “02”
HA 0-23 based hour with at least 1 digit.10 PM → “14”2 AM → “2”
HHA 0-23 based hour with at least 2 digits.10 PM → “14”2 AM → “02”
mA minute with at least 1 digit.7 → “7”29 → “29”
mmA minute with at least 2 digits.7 → “07”29 → “29”
sA second with at least 1 digit.7 → “7”29 → “29”
ssA second with at least 2 digits.7 → “07”29 → “29”

There are many more, for getting different time based on zone(z), for getting time with millisecond details(S), etc.



Got any iOS Question?