3

I need current date if the system date in past.Because if iam changing system date past my datepicker is also changed to past date but i don't want to change datepicker to my system date.My datepicker should work on current date not on system date And my datepicker should not enable for pastdates

This is my date picker code:

var usTimeZoneVal = new Date().toLocaleString('en-US', { timeZone: 'America/New_York' });
            $("#USdate").val(indianTimeZoneVal);
            $('#datetimepicker2 , #datetimepicker3')
                .datepicker({
                        autoclose: true,
                        todayHighlight: true,
                        format: 'yyyy/mm/dd',
                        //startDate: '+0d'
                        startDate: usTimeZoneVal
                })
                .on('changeDate', function (e) {
                    $('#datetimepicker2').datepicker('hide');
                    $('#GuestSearchForm').bootstrapValidator('revalidateField', 'Servicedate');
                });

And this is my Controller code:

public ActionResult GetServiceProviders(RankedServices.Entities.Admin.Services Services)
        {
            // if (Services != null && Services.SelectedServiceIds != null)

            string ServiceDate = Services.Servicedate.ToString("MM/dd/yyyy");/*Start Added by Arun 13-April-2017*/
            DateTime USDates = Convert.ToDateTime(Services.USdate);
            string USDate = USDates.ToString("MM/dd/yyyy");

            int FutureDate = DateTime.Compare(Convert.ToDateTime(ServiceDate), Convert.ToDateTime(USDate));/* End*/

            if (Services != null && (Services.SelectedServiceIds != null || Services.ServiceIds != null) && FutureDate >= 0)
            {
                if (Services.SelectedServiceIds != null)
                    Services.ServiceIds = string.Join(",", Services.SelectedServiceIds);

                //  Services.ServiceIds = string.Join(",", Services.SelectedServiceIds);zz

                if (Services.ServiceIds != "" && (Services.SubLocationID != "" || Services.Servicedate.Date != null))
                {
                    string UserID = "";
                    if (Session["UserID"] != null)
                    {
                        UserID = Session["UserID"].ToString();
                    }

                    Services.lstServiceProviders = ServiceDetails.GetServiceProviders(Services.SubLocationID, Services.ServiceIds.TrimEnd(','), UserID, ServiceDate, Services.Daymode, Services.ProviderID);

                    IEnumerable<RankedServices.Entities.Admin.Services> lstServices = ServiceDetails.GetServicesList(Services.SubLocationID.ToString());
                    ViewBag.SelectedServices = new MultiSelectList(lstServices, Services.SelectedServiceIds);

                    return View("ServicesList", Services);
                    //  return Json(lst, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return RedirectToAction("guestsearch", "Home");
                }
            }
            else
            {
                return RedirectToAction("guestsearch", "Home"); // if none are selected re-direct to Guest-Search
            }
        }
Arun Raju
  • 139
  • 2
  • 15
  • 1
    Ask a [TimeServer](http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c) – TaW Apr 14 '17 at 08:34

2 Answers2

2

The only option you have other than system time is using an online time API. There are a few free ones you can chose from. Maybe this will help.

Emad
  • 3,809
  • 3
  • 32
  • 44
1

You can create an ApiController, with a method that gets servers date, then using ajax request you get your data in javascript. You have here an example:

public class YourApiController : ApiController
{
    [HttpGet]
    [Route("api/getDateFromServer")]
    public DateTime GetDateFromServer()
    {
        return DateTime.Now;
    }
}

Then, in your javascript you add this:

$.ajax({
    type: 'GET',
    context: this, 
    url: '/api/getDateFromServer',
}).done(function (data) {
   //here you have date from server
   serverDate = data;
});
Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
Marius Orha
  • 670
  • 1
  • 9
  • 26
  • Thank u for this answer iam getting exact time but i have changed system date to past now it is 10-April-2017 but i want 14-April-2017 – Arun Raju Apr 14 '17 at 13:25
  • 1
    That's because the server runs on your machine, and changing date will impact both browser and server. – Marius Orha Apr 14 '17 at 13:31