我想将字符串“var = 02-24-2013 12:40:00.0转换为java脚本日期对象?

时间:2013-02-13 06:11:15

标签: javascript

<span id="datePickerSpan${email.id}">
<g:formatDate date="${scheduleDate}" format="dd-MM-yyyyHH:mm"/>
</span>
<input type="text" id="datePicker${email.id}" style="display:none"/>
<span onclick="openDateTimePicker('${email.id}')"></span>    

<g:javascript>

    var str='${scheduleDate}';//2013-02-2412:40:00.0
    str = str.replace(/(\d{2}:\d{2}:\d{2}).*$/g, '$1');
    alert("Str is=> "+str);//2013-02-24 12:40:00
    var date2=new Date(str);
    alert(" yy dd mm alert"+date2);//invalid date object
    str = str.replace(/^(\d{4})-(\d{2})-(\d{2})/g, '$2-$3-$1');
    alert("now new Str is =>"+str);//02-24-2013 12:40:00.0
    var date=new Date(str);
    alert("now str date is =>"+date);//invalid date object
    var date1=new Date();
alert("java script date is =>"+date1);//Wed Feb 13 2013 11:16:55 GMT+0530 (India Standard Time)
    $('#datePicker${email.id}').datetimepicker({
      dateFormat: 'dd-mm-yy',
      timeFormat: 'HH:mm',
      minDateTime: new Date(),
      maxDateTime:null,
      defaultDate:new Date(),
      showOn: "button",
      buttonImage: requestPath + "/images/calendar.gif",
      buttonImageOnly: true,
      onSelect : function(dateText,obj){
            saveRescheduleDate(${email.id},dateText);
            }
     });
 </g:javascript>

2 个答案:

答案 0 :(得分:0)

使用新的Date()创建

JavaScript Date object。:

new Date("02-24-2013 12:40:00.0");

答案 1 :(得分:0)

你可以像这样声明Date对象

var dt= new Date("October 13, 1975 11:13:00")

尝试此链接以获取更多参考资料

http://www.w3schools.com/js/js_obj_date.asp