将今天的日期设置为jQuery UI datepicker中页面加载时的默认日期

时间:2015-07-14 07:16:09

标签: jquery angularjs datepicker

我正在尝试将今天的日期显示为文本框中的默认值。我试过下面的代码。它无法正常工作

Start Date : <input tgdatepicker ng-model="searchModel.startTime" id ="startTime" />
End Date   : <input tgdatepicker ng-model="searchModel.endTime" id ="endTime" />
$("#startTime").datepicker({ 
    dateFormat: "mm-dd-yy"
}).datepicker("setDate", new Date());

4 个答案:

答案 0 :(得分:0)

您可以使用以下方式设置默认日期: defaultDate:new Date(1985,00.01)

$("#startTime").datepicker({ 
    dateFormat: "mm-dd-yy",
    defaultDate: new Date(2015, 07, 01)
})

答案 1 :(得分:0)

您可以使用以下代码将默认日期设置为jquery日期选择器的今天日期:

using System;
using System.Linq;
using System.Reflection;
using Helpers.Aspects;
using PostSharp.Aspects;
using PostSharp.Extensibility;

[assembly: EmptyStringModelBindingAspect(
    AttributeTargetTypes = @"regex:[^\.]*\.Controllers\..*Controller",
    AttributeTargetTypeAttributes = MulticastAttributes.Public,
    AttributeTargetElements = MulticastTargets.Method,
    AttributeTargetMemberAttributes = MulticastAttributes.Public)]

namespace Helpers.Aspects
{
    [Serializable]
    public class EmptyStringModelBindingAspect : MethodInterceptionAspect
    {
        public override void OnInvoke(MethodInterceptionArgs args)
        {
            for (int i = 0; i < args.Arguments.Count; i++)
            {
                FixString(args, i);
                FixStringsInObjects(args.Arguments[i]);
            }

            args.Proceed();
        }

        private static void FixString(MethodInterceptionArgs args, int i)
        {
            if (args.Arguments[i] is string && args.Arguments[i] == null)
            {
                args.Arguments.SetArgument(i, string.Empty);
            }
        }

        private static void FixStringsInObjects(object obj)
        {
            if (obj == null)
            {
                return;
            }

            var type = obj.GetType();
            var properties = (from p in type.GetProperties() 
                                         let paramerers = p.GetIndexParameters() 
                                         where !paramerers.Any() 
                                         where p.PropertyType == typeof (string) && 
                                               p.CanRead && 
                                               p.CanWrite && 
                                               p.GetValue(obj, null) == null 
                                         select p).ToArray();

            foreach (var item in properties)
            {
                item.SetValue(obj, string.Empty, null);
            }
        }

        public override bool CompileTimeValidate(MethodBase method)
        {
            return !(method.Name.StartsWith("get_") || method.Name.StartsWith("set_"));
        }
    }
}

答案 2 :(得分:0)

var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
if (month < 10) {
    month = "0" + month;
};
var day = d.getDate();
$scope.date = year + "-" + month + "-" + day;

答案 3 :(得分:0)

试试这个:

$('#datepicker').datepicker('setDate', 'today');