Javascript AngularJs日期格式转换

Javascript AngularJs日期格式转换,javascript,angularjs,Javascript,Angularjs,当我点击搜索和单选按钮时,我正在转换日期格式。当我搜索日期时,它工作正常,但当我单击单选按钮时,我遇到了问题 代码Html <form action="{{URL::current()}}" ng-submit="submit(item)"> <div class="form-group"> <label class="control-label">@lang('app.date')</label> <

当我点击搜索和单选按钮时,我正在转换日期格式。当我搜索日期时,它工作正常,但当我单击单选按钮时,我遇到了问题

代码Html

<form action="{{URL::current()}}" ng-submit="submit(item)">
    <div class="form-group">
        <label class="control-label">@lang('app.date')</label>
        <div class="input-group ui-datepicker">
            <input type="text" class="form-control datepicker"
                   uib-datepicker-popup name="enddate"
                   ng-model="item.enddate" is-open="enddate_opened"
                   ng-click="enddate_opened = !enddate_opened"/>
        </div>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary">
            <i class="fa fa-search"></i> @lang('search.search')
        </button>
    </div>
    <!-- //////////Radio button//////////////////////-->
    <div class="radio">
        <label>
            <input type="radio" name="log_date" value="log_date"
                   onchange="this.form.submit()">
            @lang('product.invoice_date')
        </label>
    </div>
</form>
单击单选按钮时URL上的结果

enddate=10%2F01%2F17

单选按钮从相同的方法调用,但为什么结果会不同?

如果调用
this.form.submit()
则只调用表单的操作
ng submit
是AngularJS指令,不会使用vanilla javascript触发。但您有“提交按钮”,因此请添加一个id:

<button type="submit" id="submit-button" class="btn btn-primary">
    <i class="fa fa-search"></i> @lang('search.search')
</button>

@lang('search.search')
并通过单选按钮调用它:

<input type="radio" name="log_date" value="log_date"
       onchange="document.getElementById('submit-button').click()">

您从“item.enddate”对象获得了不同的值。
在第一个路径中(单击搜索按钮),您将获得一个日期(javascript对象)。
在第二个路径(单击单选按钮)中,您将得到一个字符串。

使用此代码:

var val = value instanceof Date ? moment(value).format('YYYY-MM-DD') : value;
你是说:它是一个日期对象吗?好的,将其转换为字符串(感谢矩.js)。否则返回与您得到的值相同的值
只有当您有正确的字符串格式时,这才是好的。如果控制台记录“value”变量,您将在第一种情况下看到一个日期对象,在第二种情况下看到一个类似于“10/01/17”的字符串。

因此,console将舒尔的值记录在您收到的格式上,并使用它制作具有特定格式的新力矩对象

这应该起作用:

var val = value instanceof Date ? moment(value).format('YYYY-MM-DD') : moment(value, 'DD/MM/YY').format('YYYY-MM-DD');

它不起作用。此外,我的代码对于
type=“submit”
电台和
复选框都可以正常工作。
var val = value instanceof Date ? moment(value).format('YYYY-MM-DD') : value;
var val = value instanceof Date ? moment(value).format('YYYY-MM-DD') : moment(value, 'DD/MM/YY').format('YYYY-MM-DD');