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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算