I am not sure how this is actually implemented in the code, but you can approximate the new target (nBits) value with data from the prior 2016th block. Note that as described above nBits is the 32-bit compact encoding of the 256-bit target value.
Here shows the new difficulty (d_new) as:
d_new = d_old * 2 weeks / t_old
where 'old' signifies the previous 2016 ...