zoukankan      html  css  js  c++  java
  • [JS]根据格式字符串分析日期(MM与自动匹配两位的09和一位的9),货币分析成浮点数

     1 var sDate = '11/2/2005 10:24:59';
     2 var sFormat = 'MM/dd/yyyy hh:mm:ss';
     3 
     4 function getDateFromFormat(dateString,formatString){
     5     var regDate = /\d+/g;
     6     var regFormat = /[YyMmdHhSs]+/g;
     7     var dateMatches = dateString.match(regDate);
     8     var formatmatches = formatString.match(regFormat);
     9     var date = new Date();
    10     for(var i=0;i<dateMatches.length;i++){
    11         switch(formatmatches[i].substring(0,1)){
    12             case 'Y':
    13             case 'y':
    14                 date.setFullYear(parseInt(dateMatches[i]));break;
    15             case 'M':
    16                 date.setMonth(parseInt(dateMatches[i])-1);break;
    17             case 'd':
    18                 date.setDate(parseInt(dateMatches[i]));break;
    19             case 'H':
    20             case 'h':
    21                 date.setHours(parseInt(dateMatches[i]));break;
    22             case 'm':
    23                 date.setMinutes(parseInt(dateMatches[i]));break;
    24             case 's':
    25                 date.setSeconds(parseInt(dateMatches[i]));break;
    26         }
    27     }
    28     return date;
    29 }
    30 
    31 function parseCurrency(currentString){
    32     var regParser = /[\d\.]+/g;
    33     var matches = currentString.match(regParser);
    34     var result = '';
    35     var dot = false;
    36     for(var i=0;i<matches.length;i++){
    37         var temp = matches[i];
    38         if(temp =='.'){
    39             if(dot) continue;
    40         }
    41         result += temp;
    42     }
    43     return parseFloat(result);
    44 }
    45 alert(getDateFromFormat(sDate,sFormat));
    46 alert(parseCurrency("¥1,900,000.12"));
  • 相关阅读:
    无线渗透开启WPS功能的路由器
    写代码怎能不会这些Linux命令?
    分布式服务框架 Zookeeper -- 管理分布式环境中的数据
    每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
    Innodb 中的事务隔离级别和锁的关系
    线上操作与线上问题排查实战
    MySQL 四种事务隔离级的说明
    一次由于 MTU 设置不当导致的网络访问超时
    SYN 和 RTO
    The story of one latency spike
  • 原文地址:https://www.cnblogs.com/think/p/550011.html
Copyright © 2011-2022 走看看