1. 程式人生 > >JS方法

JS方法

nbsp arr ear seconds nth bsp and 隨機 parseint

var time = new Date();
var year = time.getFullYear();
年份輸出
var second = time.getSeconds();
秒的輸出
var moth = time.getMonth()+1;
月份的輸出
var day = time.getDay();
星期幾
var day = time.getDate();
哪一天


document.write(time);


var rand = Math.random();
random 是隨機數的意思

document.write(rand);
var arr = ["張三","李四","王五","趙六","七七"]
var shuzi =rand*arr.lenght;
document.write(shuzi);
是0到1的數,也就是概率
shuzi = parseInt(shuzi);
取整
document.write(arr[shuzi]);
隨機數的轉換


var i = 1.1;
i = math,round(i);
document.write
四舍五入

var i = 1.1;
i = Math.ceil(i);
向上取整
i = Math.floor(i);
向下取整
document.write(i);

JS方法