~~~
//图片转化成base64
//var img = "imgurl";//imgurl 就是你的图片路径
getBase64Image = function(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
var dataURL = canvas.toDataURL("image/" + ext);
return dataURL;
}
//调用方法
// var image = new Image();
// image.src = img;
// image.onload = function(){
// var base64 = getBase64Image(image);
// console.log(base64);
// }
~~~
*****
~~~
//切割金额,超过万和亿则加万和亿的文字
cutmoney = function(money) {
if (money >= 10000000) {
money = parseFloat(money / 10000000) + '亿';
} else if (money < 10000000 && money >= 10000) {
money = parseFloat(money / 10000) + '万';
} else {
money = parseFloat(money);
}
return money;
}
~~~
*****
~~~
//判断用户名,如果没有真实姓名则用用户名
getname = function(name, turename) {
var username = name;
if (turename && turename != '' && turename != 'null') {
username = turename;
}
return username;
}
~~~
*****
~~~
//判断用户头像,如果为空则用默认头像
getuserpic = function(useravatar) {
//alert(useravatar);
var userpic = '../image/default_user_portrait.gif';
if (useravatar && useravatar != '' && useravatar != 'null') {
userpic = gip + 'data/upload/shop/avatar/' + useravatar;
}
return userpic;
}
~~~
*****
~~~
//转换unix时间戳为年月日
turntime = function(ut) {
var dateObj = new Date(ut * 1000);
var UnixTimeToDate = dateObj.getUTCFullYear() + ' 年 ' + (dateObj.getUTCMonth() + 1 ) + ' 月 ' + dateObj.getUTCDate() + ' 日 ';
return UnixTimeToDate;
}
~~~
*****
~~~
//转换时间格式字符串为时间
turnDate = function(strDate) {
var st = strDate;
var a = st.split(" ");
var b = a[0].split("-");
var c = a[1].split(":");
var date = new Date(b[0], b[1] - 1, b[2], c[0], c[1], c[2]);
return date;
}
~~~
*****
~~~
//转换unix时间戳为年月日小时分钟
turntimemore = function(ut) {
var dateObj = new Date(ut * 1000);
var UnixTimeToDate = dateObj.getUTCFullYear() + ' 年 ' + (dateObj.getUTCMonth() + 1 ) + ' 月 ' + dateObj.getUTCDate() + ' 日 ' + dateObj.getUTCHours() + ':' + dateObj.getUTCMinutes();
return UnixTimeToDate;
}
~~~
*****
~~~
//去掉调用的null
qunull = function(str) {
if (!str || str == "null" || str == "Null" || typeof (str) == "undefined" || str == "undefined" || str == "NULL") {
return '';
} else {
return str;
}
}
~~~
~~~
//获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
getnowDatetime = function() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
return currentdate;
}
~~~
*****
~~~
//指定时间后的时间(特定格式)
getnowDatetime1 = function(s) {
var cdate = '';
cdate = new Date(new Date().getTime() + s);
var month = cdate.getMonth() + 1;
var strDate = cdate.getDate();
var strHour = cdate.getHours();
var strMinu = cdate.getMinutes();
var strSeco = cdate.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (strHour >= 0 && strHour <= 9) {
strHour = "0" + strHour;
}
if (strMinu >= 0 && strMinu <= 9) {
strMinu = "0" + strMinu;
}
if (strSeco >= 0 && strSeco <= 9) {
strSeco = "0" + strSeco;
}
var currentdate = cdate.getFullYear() + month + strDate + strHour + strMinu + strSeco;
//alert(currentdate);
return currentdate;
}
~~~
*****
~~~
//获取当前的日期 格式“yyyy-MM-dd”
getnowDate = function() {
var date = new Date();
var seperator1 = "-";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
return currentdate;
}
~~~
*****
~~~
//获取指定的日期 格式“yyyy-MM-dd”
getoldDate = function(ot) {
var date = new Date(ot);
var seperator1 = "-";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
return currentdate;
}
~~~