I think it depends on your threshold for hop aroma/flavor.
I would start with 7 days and sample the beer. If it needs more then let them sit for another 7 days and sample again...and so on. When you get to the level you like then remove them.
Some folks leave them in for the duration of the keg, but keep in mind that there can be an aquired "vegetal taste" imparted by the hops over time, depending on the quantity of hops and level of exposure to the beer.
As was already said, I also use a bag but I use muslin cloth for both whole and pellet hops. Dental floss works well for retrieval.