js怎么将var转为string

JavaScript020

js怎么将var转为string,第1张

var str="123"上面就是字符串类型的赋值。javascript中申明变量都用var,是弱类型,不管整型,对象,数组还是字符串都用var申明。是什么类型决定于你对这个变量赋了什么值。比如 var s1 = 1//这时s1是numbers1=1+'1'//这是s1是string,结果是11

用 String 函数 或 toString 方法:

alert(String(123)) var a = 123

alert(a.toString())

js中将日期转换成字符串,如下操作:

定义日期: Date 对象用于处理日期和时间。 可以通过 new 关键词来定义 Date 对象。以下代码定义了名为 myDate 的 Date 对象: var myDate=new Date() 注释:Date 对象自动使用当前的日期和时间作为其初始值。 var myDate=new Date(2013-1-10) Javascript 获取当前时间戳(毫秒级别): 第一种方法:var timestamp1 = Date.parse( new Date())结果:1372751992000 第二种方法:var timestamp2 = ( new Date()).valueOf()结果:1372751992066 第三种方法:var timestamp3 = new Date().getTime()结果:1372751992066 alert(timestamp1)alert(timestamp2)alert(timestamp3)备注:第一种获取的时间戳是把毫秒改成000显示,第二种和第三种是获取了当前毫秒的时间戳。