Two methods, one choice you have to make upfront
Google Sheets stores dates as serial numbers internally — a floating-point count of days since December 30, 1899. When you call setNumberFormat on a range, you are telling Sheets how to render that number. The underlying value stays a real date, so DATEDIF, date arithmetic, and pivot table grouping all keep working. That is the right call for 90% of cases.
Utilities.formatDate does something different: it converts the Date object JavaScript hands you into a plain string, which then gets written back to the cell. The cell now holds text, not a date. DATEDIF will return a #VALUE! error, and sorting will be lexicographic. I have burned a solid afternoon debugging a broken pivot because I reached for Utilities.formatDate out of habit when setNumberFormat was all I needed.