zoukankan      html  css  js  c++  java
  • js 日期和Date的相互转换

    //日期  /.-转Date  Date转/.-默认-
    function StringToDate(DateStr)  { 
    	var separator="-";
    	if(DateStr.indexOf("/")>-1)separator="/";
    	if(DateStr.indexOf(".")>-1)separator=".";
        var converted = Date.parse(DateStr);  
        var myDate = new Date(converted);  
        if (isNaN(myDate)){   
            var arys= DateStr.split(separator);  
            myDate = new Date(arys[0],arys[1]-1,arys[2]);  //人类的习惯 month就是几月
        }  
        return myDate;  
    }
    function DateToString(date,separatorStr){
    	var separator="-";
    	if(separatorStr)separator=separatorStr;
    	if(typeof date=="string")date=StringToDate(date);
    	return date.getFullYear()+separator+(date.getMonth()+1)+separator+date.getDate();
    }
    console.log(StringToDate("2012-03-09"));
    console.log(StringToDate("2012-3-9"));
    console.log(StringToDate("2012/3/9"));
    console.log(StringToDate("2012.03.09"));
    console.log(DateToString(new Date()));
    console.log(DateToString("2012-03-09","."));
    

  • 相关阅读:
    css3记事
    ele
    vue记事1
    HBuilder
    继承与面向对象设计
    实现
    设计与声明
    资源管理
    构造/析构/赋值运算
    让自己习惯C++
  • 原文地址:https://www.cnblogs.com/frostbelt/p/2388692.html
Copyright © 2011-2022 走看看