0

I have an input field which stores data in format

28 Oct 2016 10:50 pm

I need to send the values to server in different format, I just need to send day of week and time in 22:50:00 format.

Output required:

var dayOfWeek =  6 // (or friday); 
var time = 22:50:00;

I am struggling to do it in a simple and neat way. Any help would be appreciated.

Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50

3 Answers3

4

Try this

(function myFunction() {
    var d = new Date("28 Oct 2016 10:50 pm");
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
   var form="";
    var n = weekday[d.getDay()];
  var time = d.getHours()+':'+d.getMinutes();
  if(d.getHours() > 12)
    {form = "pm"}else{form ="am"}
   console.log( n+'    '+time+form);
})()
<p id="demo"></p>
prasanth
  • 22,145
  • 4
  • 29
  • 53
4

If you can do it on server side: check strtotime() for php http://php.net/manual/en/function.strtotime.php

var_dump(date('l H:i:s', strtotime('28 Oct 2016 10:50 pm')));

This will return string(11) "Friday 22:50:00"

Vlad K
  • 145
  • 8
  • He has to send it to server. Which means he is giving option to change date. So no. This cannot be used – Rajesh Oct 27 '16 at 07:48
0

If you want your expected days of month and time(24 hour format) Then try it:

var dt = new Date("28 Oct 2016 10:50 pm");
var day = dt.getDay();  //days of the week

alert("Days of the week: "+day);

var time = fixLength(dt.getHours())+":"+fixLength(dt.getMinutes())+":"+fixLength(dt.getSeconds());

alert("Time: "+time);



function fixLength(x){
   return (x<10?'0':'')+x; 
}
AHJeebon
  • 1,218
  • 1
  • 12
  • 17