douwulu2576 2014-04-24 09:09
浏览 37
已采纳

在javascript“dd monthstring,yyyy”中解析datepicker日期

Is there an easier way of getting a datetime from a string such as this: "29 March, 2014" in JAVASCRIPT

I've done it within PHP, but I want to fiddle with some front end to add days to the date etc..

Here is the required PHP code to achieve this:

function removeDateComma($string){
    $without_comma = preg_replace('/,/', '', $string);
    return $without_comma;
}

function getYearMonthDayFromDate($string){
    $array  = array();
    $string = removeDateComma($string);
    $string = strtotime($string);
    $year   = date('Y', $string);  
    $month  = date('m', strtotime("-1 month", $string));  
    $day    = date('d', $string);
    $array['year'] = $year;
    $array['month'] = $month;
    $array['day'] = $day; 
    return $array;
}

Calling this to get the required datetime:

$from = getYearMonthDayFromDate($_REQUEST['from_date']);

Can anyone come up with a solution to this problem? Getting the same result but in javascript?

Edit:

It's already implemented in PHP. I need to do it at the front end i.e. no form submission, I cant send anything anywhere, i've got to get this date from an input field, i.e. var field = document.getElementById("input_from").value; so that will be in the format 11 Month, 2014. So i've got this string sitting here which is useless, I need to get each individual value, i.e. the year: 2014, month: 03 or 3 and day: 11

Thanks!

  • 写回答

2条回答 默认 最新

  • doulu1907 2014-04-24 09:36
    关注

    Try this

     function getMonthName( monthNumber ) 
        {
            var monthNames = new Array("January", "February", "March", 
          "April", "May", "June", "July", "August", "September", 
          "October", "November", "December");
          return monthNames[monthNumber ]; 
        }
        var field = $("#input_from").val();
    
         var date1 = new Date(field); 
            var cMonth = date1.getMonth();
            var newdate = getMonthName(cMonth)   + " "+ date1.getDate() + ", " + date1.getFullYear();
    alert(newdate);
    

    Fiddle: http://jsfiddle.net/chetangawai/MNbjw/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法