How does one subtract hours from an NSDate?

swift subtract days from date
swift date minus 1 second
how to add one hour to date swift
date minus seconds swift
swift 4 add hours to date
add two hours to date swift
swift date add 1 second
swift add one minute to date

I would like to subtract 4 hours from a date. I read the date string into an NSDate object use the following code:

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate * mydate = [dateFormatter dateFromString:[dict objectForKey:@"published"]];

What do I do next?

NSDate *newDate = [theDate dateByAddingTimeInterval:-3600*4];

Link to documentation.


NSDate *newDate = [[[NSDate alloc] initWithTimeInterval:-3600*4
                                              sinceDate:theDate]] autorelease];

Link to documentation.

objective c - How to subtract 1 hour from a given date/time, How does one subtract hours from an NSDate? (7 answers). Closed 6 years ago. I want to get the date / time an hour before a given date / time. Let's say the  Create two NSDate objects from the strings using NSDate's -dateWithString:, then get the difference of the two NSdate objects using . NSTimeInterval diff = [date2 timeIntervalSinceDate:date1];

NSCalendar is the general API for changing dates based on human time units. For this, you can use NSCalendar's -dateByAddingComponents:toDate:options: with a negative number of hours.

How to subtract 1 hour from current time using Swift?, To subtract hours from a date in swift we need to create a date first. Once that date is created we have to subtract hours from that, though swift does not provide a way to subtract date or time, but it provides us a way to add date or date component in negative value. However, if you use it to add a day to the 31st March 2013, it'll return a date that has only 23 hours added to it. It may well actually have the 24, but using in calculations has only 23:00 hours added.

Since iOS 8 there is the more convenient dateByAddingUnit:

Swift 2.x

//subtract 3 hours
let calendar = NSCalendar.autoupdatingCurrentCalendar()
newDate = calendar.dateByAddingUnit(.Hour, value: -3, toDate: originalDate, options: [])

How to work with dates and times in Swift 3, part 4: Adding Swift , They are: Date, which represents a single point in time, […] want to find out what the date and time will be 2 months, 3 days, 4 hours, 5 minutes, Now that we can add and subtract DateComponents , let's overload the unary  Select add or subtract. The hours entered must be a positive number or zero (0). The minutes entered must be a positive number or zero (0). Click "Click to Calculate" button. The total will appear. If a greater number of hours and minutes is subtracted from a lesser number of hours and minutes the result will be a negative number.

//in Swift 3
//subtract 3 hours
let calendar = NSCalendar.autoupdatingCurrent
newDate = calendar.date(byAdding:.hour, value: -3, to: originalDate)

Calculator: Add to or subtract from a date and time, This calculator tool will enable you to add or subtract seconds, minutes, hours, days, weeks, months and years from a given date. duration, with both date and time included; Birthday Calculator – Find when you are 1 billion seconds old  Time of the Day Calculator. Using 24 hours format, add or subtract the time in a day, and returns the result time in a day. For Example: at 08:22:43, adds 2hr + 41min + 23sec is 11:04:06 in the same day. at 04:14:29, subtract 20hr - 5min - 32sec is 08:08:57 another day.

In Swift 4 :

var baseDate = ... // something
let dateMinus4Hours = Calendar.current.date(byAdding: .hour, value: -4, to: baseDate)

don't go with 24*3600 and stuff, that's asking for trouble.

Swift NSDate utils: Get date by adding / subtracting days from now , Swift NSDate utils: Get date by adding / subtracting days from now, date at midnight, . Embed. What would you like to do? date = NSDate.​dateBySubstractingDays(1) Hour, .Minute, .Second], fromDate: date). // Specify date components. To subtract time that’s more than 24 hours: It is necessary to create a formula to subtract the difference between two times that total more than 24 hours. Follow the steps below: Referring to the above example, select cell B1 and drag to cell B2 so that you can apply the format to both cells at the same time.

Subtract hours from a time and date using PHP., This is a tutorial on how to subtract hours from a date and time using PHP. You can subtract hours by using PHP's time function and some basic This works because there are exactly 3600 seconds in one hour (60 x 60  Convert DateTime to NSDate and back Xamarin has truly been an impressive presence in the world of mobile over the last few years, and is only going from strength to strength. It has given C# developers the chance to transfer their skills to the mobile realm without having to have any knowledge of objective-c.

Add or subtract hours from a Date/Time field, Knowledge Article Number, 000321563. Description. The syntax you can use to add/subtract hours from a Date/Time field is detailed below. Resolution. The most common operation when using date ( NSDate) objects in applications, is to convert them into string objects so they can be properly formatted and shown to the users. Quite common is the reverse action as well; converting strings to date objects. However, these are not the only tasks regarding dates.

Adding and Subtracting Hours in ETL, Use the Date Operations action. If you are ADDING hours, add 1 day to the "Date" column; if you are SUBTRACTING hours, subtract 1 day from  For subtracting 12 hours from a date/time field, you can apply this formula =A2-12/24.

Comments
  • (It's now 6 years, and 4 major versions of XCode later... and NSDate's functionality is still as unintuitive as in 2009 !!)
  • NSCalendar is not a recommended API to use. developer.apple.com/documentation/Cocoa/Reference/Foundation/…
  • @norskben: That page is for NSCalendarDate. That's what it says not to use. It specifically recommends NSCalendar as a replacement in the same note.
  • Comment says subtract however 3 was used as value it should instead be -3