Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active November 20, 2022 22:17
Show Gist options
  • Save luislobo14rap/ffa188844e16c3d0bec101e403b822f7 to your computer and use it in GitHub Desktop.
Save luislobo14rap/ffa188844e16c3d0bec101e403b822f7 to your computer and use it in GitHub Desktop.
get-time-in-string.js
// 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;
};
@luislobo14rap
Copy link
Author

getDate() + '-' + (getMonth() + 1) + getFullYear()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment