function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值
}
上面的例子是取[min, max)
左闭右开区间的任意数字,假如取[0, 100)
之间的随机数,是取不到100
的。Math.random() =>
取[0, 1)
之间的任意随机数字。
怎么处理才能取到100
呢?也就是怎么变成闭区间呢?
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //不含最大值,含最小值
}
以min = 0, max = 100
,为例:
Math.random() * (100 - 0 + 1) => [0, 101)
Math.floor([0, 101)) => 0,1,2,...,100
Math.floor([0, 101)) + 0 => 0,1,2,...,100
js将毫秒数转化为时间
// 根据毫秒数构建 Date 对象
var date = new Date(1499996760000);
// 格式化日期
dateTime = date.toLocaleString();
这时候 dateTime
的值为"2018/07/10 下午2:07:02
"。可以通过重写 toLocaleString()
方法,来自定义日期显示格式。
// 重写方法,自定义格式化日期
Date.prototype.toLocaleString = function() {
// 补0 例如 2018/7/10 14:7:2 补完后为 2018/07/10 14:07:02
function addZero(num) {
if(num<10)
return "0" + num;
return num;
}
// 按自定义拼接格式返回
return this.getFullYear() + "/" + addZero(this.getMonth() + 1) + "/" + addZero(this.getDate()) + " " +
addZero(this.getHours()) + ":" + addZero(this.getMinutes()) + ":" + addZero(this.getSeconds());
};
// 根据毫秒数构建 Date 对象
var date = new Date(1499996760000);
// 按重写的自定义格式,格式化日期
dateTime = date.toLocaleString();
js毫秒数转换成时间格式
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
{
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
console.log((new Date(毫秒数)).Format("yyyy-MM-dd hh:mm:ss"))
console.log(new Date(毫秒数).Format("yyyy-MM-dd hh:mm:ss:S"))
将Date
对象转化成毫秒数:
var a = new Date().getTime();
console.log(a);
这样就可以随意转化成我们想要的功能了。
js
中原声js时间函数方法:
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
慌慌张张 匆匆忙忙 为何生活总是这样 难道说我的理想 就是这样度过一生的时光 不卑不亢 不慌不忙 也许生活应该这样 难道说六十岁以后 再去寻找我想要的自由 其实我也常对自己说 人要学会知足而常乐 可万事都一笑而过 还有什么意思呢
js 日期格式转换(毫秒转日期)
new Date(毫秒值).format("yyyy-MM-dd hh:mm:ss");
// ---------------------------------------------------
// 日期格式化
// 格式 YYYY/yyyy/YY/yy 表示年份
// MM/M 月份
// W/w 星期
// dd/DD/d/D 日期
// hh/HH/h/H 时间
// mm/m 分钟
// ss/SS/s/S 秒
// ---------------------------------------------------
Date.prototype.format = function(formatStr)
{
var str = formatStr;
var Week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
str=str.replace(/MM/,(this.getMonth()+ 1)>9?(this.getMonth()+ 1).toString():'0' + (this.getMonth()+ 1));
str=str.replace(/M/g,this.getMonth() + 1);
str=str.replace(/w|W/g,Week[this.getDay()]);
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
str=str.replace(/d|D/g,this.getDate());
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes());
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds());
return str;
}
Moment.js
进行时间类型转换
初始化
初始化为当前时间:
let now = moment();
等价于 moment(new Date());
使用时间戳初始化:
moment(number); // number为毫秒
moment.unix(number); //number为秒
使用JS Date对象初始化
let date = new Date();
moment(date);
moment
对象转换成时间戳
let time = moment.valueOf();