0

I am trying to get some calendar items based on the StartTime field.

I've done this:

/_vti_bin/ListData.svc/MyAgenda?$filter=StartTime%20gt%20datetime%272015-03-16T19:00:00Z%27

This show me the correct result. The problem is that when I am trying to convert this date in JavaScript I am getting the wrong date.

In Edit page of the list I see the StartTime field like this:

Start Time - 3/17/2015 12:00 PM

Can anyone help me on this?

1

The timestamp in your question is called Epoch Date. It is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

To convert this you can use JavaScript Date object.

You can do that using following code

var myDate = new Date(1426593600000);
alert(myDate.toGMTString()+"<br>"+myDate.toLocaleString());
4
  • This way it is showing the correct hour. Could you please let me know how can I get the day hours, minutes etc? When I try to do so, It gets the wrong value... Thanks!
    – leonardo
    Mar 17 '15 at 13:07
  • var myDate = new Date(1426593600000); alert(myDate.getHours()); alert(myDate.getMinutes()); for more on date object w3schools.com/jsref/jsref_obj_date.asp Mar 17 '15 at 13:10
  • This way it is showing a wrong value. If you take a look at the myDate.toGMTString() you will see that the hour is 12
    – leonardo
    Mar 17 '15 at 13:11
  • Ok in that case you can use getUTCHours() Mar 17 '15 at 13:20
0

You can use this method.

var startdate = new Date(#your date); // #your date = startdate
      var a_p = "";
      var curr_hour = startdate.getHours();
      if (curr_hour < 12) {
          a_p = "am";
      }
      else {
          a_p = "pm";
      }
      if (curr_hour == 0) {
          curr_hour = 12;
      }
      if (curr_hour > 12) {
          curr_hour = curr_hour - 12;
      }
      var curr_min = startdate.getMinutes();
      curr_min = curr_min + "";

      if (curr_min.length == 1) {
          curr_min = "0" + curr_min;
      }
      startdate = [(startdate.getMonth() + 1).padLeft(), startdate.getDate().padLeft(), startdate.getFullYear()].join('/') + ' ' + [curr_hour, curr_min].join(':') + ' '+ [a_p] ; 

it will give you the result in this format: 10/02/2014 10:30 am ie mm/dd/yyyy hh:mm in 12 hour format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.