js 时间YYYY-MM-DD转换为YYYY/MM/DD 自定义函数格式

时间:2023-11-22 21:47:32
<script type="text/javascript">
        window.onload = function () {
            var NumArray = [];
            while (NumArray.length < 6) {
                var NumRand = parseInt(Math.random() * 15) + 10;
                var IsCun = 0;
                for (var i = 0; i < NumArray.length; i++) {
                    if (NumArray[i] == NumRand) {
                        IsCun = 1;
                    }
                }
                if (IsCun == 0) {
                    NumArray[NumArray.length] = NumRand;
                }
            }
            for (var j = 0; j < NumArray.length; j++) {
                document.write(NumArray[j] + "<br/>");
            }
            var DateStr = "2014-01-28";
            var myDate = new Date(DateStr);
            document.write("时间为1:" + myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate() + "<br/>");
            document.write("时间为2:" + ConverToDates(DateStr) + "<br/>");
            document.write("时间为3:" + ConverToDate(DateStr) + "<br/>");
        }
        //自定义函数1 时间转换
        function ConverToDates(DateStr) {
            var myDate = new Date(DateStr);
            var strDate = myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate();
            return strDate;
        }
  //自定义函数2 时间转换
        function ConverToDate(DateStr) {
            if (/^\d{4}-\d{2}-\d{2}$/.test(DateStr)) {
                var date = DateStr;
                DateStr = date.replace(/\-/g, "/");
            }
            return DateStr;
        }
    </script>