I want to get the date from NSDate
and do not need time. Example:
NSDate* today = [NSDate date]; // want to give 20-06-2012 only
I want to get the date for today without showing the time and use it in NSDate
variable or string variable.
In case someone else looks for this. I used the following:
NSDateComponents *components = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:[NSDate date]];
NSDate *startDate = [[NSCalendar currentCalendar]
dateFromComponents:components];
In terms of performance this approach may be better than using NSDateFormatter or NSCalendar.
Swift 3, 4, 5
let timeInterval = floor(Date().timeIntervalSinceReferenceDate / 86400) * 86400
let newDate = Date(timeIntervalSinceReferenceDate: timeInterval)
Swift 2
let timeInterval = floor(NSDate().timeIntervalSinceReferenceDate / 86400) * 86400
let newDate = NSDate(timeIntervalSinceReferenceDate: timeInterval)
You can also take into account time zone (this function is part of NSDate extension):
Swift 3, 4, 5
extension Date {
func dateWithoutTime() -> Date {
let timeZone = TimeZone.current
let timeIntervalWithTimeZone = self.timeIntervalSinceReferenceDate + Double(timeZone.secondsFromGMT())
let timeInterval = floor(timeIntervalWithTimeZone / 86400) * 86400
return Date(timeIntervalSinceReferenceDate: timeInterval)
}
}
Swift 2
extension NSDate {
func dateWithoutTime() -> NSDate {
let timeZone = NSTimeZone.localTimeZone()
let timeIntervalWithTimeZone = self.timeIntervalSinceReferenceDate + Double(timeZone.secondsFromGMT)
let timeInterval = floor(timeIntervalWithTimeZone / 86400) * 86400
return NSDate(timeIntervalSinceReferenceDate: timeInterval)
}
}
Use This method
- (NSString *)curentDateStringFromDate:(NSDate *)dateTimeInLine withFormat:(NSString *)dateFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:dateFormat];
NSString *convertedString = [formatter stringFromDate:dateTimeInLine];
return convertedString;
}
Use it Like Below
NSString *dateString = [self curentDateStringFromDate:[NSDate date] withFormat:@"dd-MM-yyyy"];
I implemented a Swift extension if anyone is interested. This will give you the current date (in your timezone) at 00:00:00 UTC, which I think is the meaning of current date without time.
I think it's better than @Murlakatam's answer because it doesn't involve messing with the raw timeIntervalSinceReferenceDate
which is why we use date objects in the first place.
extension NSDate
{
// Calendar objects are expensive to create to it's better to create and reuse only one.
private static var cachedNoTimeZoneCalendar: NSCalendar = {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
return calendar
}()
func dateWithoutTime() -> NSDate?
{
let dateComponents = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: self)
return NSDate.cachedNoTimeZoneCalendar.dateFromComponents(dateComponents)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With