Last active
May 17, 2017 04:00
-
-
Save phongvh/bc324b744e036b267fa46ea9ab61cb0b to your computer and use it in GitHub Desktop.
A collection of useful Javascript functions and snippets
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
/* | |
* Convert a number into a string represents Vietnamese Money format (1,000 đ) | |
* | |
* @param A number | |
* @return A string represents Vietnamese Money format (1,000 đ) | |
*/ | |
function formatMoney(number) { | |
if (!number) return number; | |
number = number.toString().replace(/[^\d\.]/g, ''); | |
number = number.replace(/(\.)(\d*)(\.)/, '$1$2'); | |
if (number.indexOf('.') !== -1) { | |
var part = number.split('.'); | |
number = part[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.') + ',' + part[1].replace(/(\d)\d(\d)/, '$1$2'); | |
} else | |
number = number.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.'); | |
return number + ' đ'; | |
} | |
/* | |
* Convert a unix timestamp into a string represents time in hh:mm:ss format | |
* | |
* @param A unix timestamp (in seconds) | |
* @return A string represents time in hh:mm:ss format | |
*/ | |
function tsToTime (unix_timestamp) | |
{ | |
// Create a new JavaScript Date object based on the timestamp | |
// multiplied by 1000 so that the argument is in milliseconds, not seconds. | |
var date = new Date(unix_timestamp*1000); | |
// Hours part from the timestamp | |
var hours = date.getHours(); | |
// Minutes part from the timestamp | |
var minutes = "0" + date.getMinutes(); | |
// Seconds part from the timestamp | |
var seconds = "0" + date.getSeconds(); | |
// Will display time in 10:30:23 format | |
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment