Last active
November 20, 2022 22:17
-
-
Save luislobo14rap/ffa188844e16c3d0bec101e403b822f7 to your computer and use it in GitHub Desktop.
get-time-in-string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get-time-in-string.js v1.1 | |
function getTimeInString(text, newDate = new Date()){ | |
let _fullYear = newDate.getFullYear() | |
, _month = newDate.getMonth() + 1 | |
, _date = newDate.getDate() | |
, _hours = newDate.getHours() | |
, _minutes = newDate.getMinutes() | |
, _seconds = newDate.getSeconds(); | |
_month = ( _month > 9 ? _month : '0' + _month.toString() ) | |
_date = ( _date > 9 ? _date : '0' + _date.toString() ) | |
_hours = ( _hours > 9 ? _hours : '0' + _hours.toString() ) | |
_minutes = ( _minutes > 9 ? _minutes : '0' + _minutes.toString() ) | |
_seconds = ( _seconds > 9 ? _seconds : '0' + _seconds.toString() ) | |
text = text | |
.replace(/year/g, _fullYear ) | |
.replace(/month/g, _month ) | |
.replace(/day/g, _date ) | |
.replace(/hours/g, _hours ) | |
.replace(/minutes/g, _minutes ) | |
.replace(/seconds/g, _seconds ); | |
return text; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getDate() + '-' + (getMonth() + 1) + getFullYear()