Any person who is sentenced to a term of confinement in a correctional facility shall have deducted from any such term all time actually spent by the person in a state hospital for examination purposes or treatment prior to trial, in a state or local correctional facility awaiting trial or pending an appeal, or in a juvenile detention facility awaiting trial for an offense for which, upon conviction, such juvenile is sentenced to an adult correctional facility. Such credit for time shall include any time spent in pretrial confinement or detention on separate, dismissed, or nolle prosequi charges that are from the same act as the violation for which the person is convicted and sentenced to a term of confinement. When entering the final order in any such case, the court shall provide that the person so convicted be given credit for the time so spent.
In no case shall a person be allowed credit for time not actually spent in confinement or in detention. In no case is a person on bail to be regarded as in confinement for the purposes of this statute. No such credit shall be given to any person who escapes from a state or local correctional facility or is absent without leave from a juvenile detention facility.
Any person sentenced to confinement in a correctional facility, in whose case the final order entered by the court in which he was convicted fails to provide for the credit authorized by this section, shall nevertheless receive credit for the time so spent in a correctional facility. Such allowance of credit shall be in addition to the good conduct allowance provided for in §§ 53.1-116 and 53.1-129, Articles 2 (§ 53.1-192 et seq.) and 3 (§ 53.1-198 et seq.) or the earned sentence credits provided for in Article 4 (§ 53.1-202.2 et seq.).
Va. Code § 53.1-187