-2

Javascript:I have a function which has s parameter s contains this s=07:05:45PM;s has time which is in form of string i want to use it in new Date() but gives error i had to get hours mins seconds convert this time to 24 hour format please help me output:invalid date

function time Conversion(s) { 
   var date=new Date(s);    
   console.log(date); 
}
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • You are giving time and want it to recognize as a date? besides, new date(String) will need string that is recognized by Date.parse() method. – Aswin May 18 '18 at 07:11
  • My understanding is you want to convert the given time string to 24hrs format. right? – Libin C Jacob May 18 '18 at 07:22
  • yes i want to convert in 24hrs format –  May 18 '18 at 08:49
  • Possible duplicate of [Javascript datetime string to Date object](https://stackoverflow.com/questions/12953302/javascript-datetime-string-to-date-object) – Mast May 21 '18 at 16:17

5 Answers5

2

According to specification, you can pass the dateString as a parameter to the Date constructor. There is a bunch of dateString format limitations, and in you case your dateString (named s) is invalid for date constructor (actually, your s is even has not any date, it consists of time only).

The possible solution is to handle your s parameter manually: cut verbal part, split time by :, then pass params to the Date constructor in sequence year, month, date, hours, minutes, seconds, or construct your own ISO String, format:

{year}-{month}-{date}T{hours}:{minutes}:{seconds}.{milliseconds}Z

Note, that hours in both cases should be in 24-hours format, so you should manually handle your 12-h formatted hours.

Limbo
  • 2,123
  • 21
  • 41
  • The OP has a time with no date. The suggested string format will be parsed as UTC, code for how to handle that should be included. Also, there is no assistance with converting the time to 24 hour format (which is also required for the suggested format), which is the core of the question. – RobG May 19 '18 at 12:51
  • @RobG well, thank you for you comment. I will update my answer as soon as I'll have free time. – Limbo May 19 '18 at 13:02
  • Oh, your reference to the "specification" is actually to MDN, which is a public wiki. The actual specification is ECMA-262, the current edition is [*ECMAScript 2017*](http://ecma-international.org/ecma-262/8.0/) and soon to be ECMAScript 2018. ;-) – RobG May 19 '18 at 13:07
  • @RobG ...and all necessary links to the "REAL SPECIFICATION" are present on MDN. The difference is that on MDN all information is more easy to understand. – Limbo May 19 '18 at 13:19
  • Sure, but while MDN is a great resource, it isn't a specification. It describes itself as a [*JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/About). :-) – RobG May 19 '18 at 20:58
1

The reason you're getting an invalid Date from the built-in parser is covered by Why does Date.parse give incorrect results?

To convert a string like 07:05:45PM to 24 hour time, you can parse the parts and generate an new string, adding 12 to the hour if it ends in PM or not if it ends in AM (and change 12am to 00). e.g.

function to24HrFormat(s) {
  var z = n => (n<10?'0':'')+n;
  var b = s.match(/\d+/g);
  var ap = /am$/i.test(s)? 0 : 12;
  return z((b[0]%12) + ap) + ':' + b[1] + ':' + b[2];
}

// Tests
['07:05:45PM', '06:23:49AM', '12:15:00AM', '11:59:59pm']
  .forEach(s => console.log(s + ' => ' + to24HrFormat(s)));

You should validate the input string, and maybe allow for missing seconds.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

From the time string, remove the AM/PM (need to have a 24hr timestring)

var time = "07:05:45";
var datetime = new Date('1970-01-01T' + time + 'Z');

Now do your time based operations.

NitinSingh
  • 2,029
  • 1
  • 15
  • 33
  • Using the OP string of "07:05:45PM" produces an invalid date in Safari, Chrome and Firefox at least. If the PM is removed the time is wrong, and the addition of "Z" means it will be parsed as UTC, so shifts the time by the local timezone offset. You might want to include how to handle all that. – RobG May 19 '18 at 13:10
  • Tried that in latest Chrome before posting – NitinSingh May 19 '18 at 13:12
  • Try `new Date('1970-01-01T' + '07:05:45PM' + 'Z');` which is what the OP has. Without the am/pm, it should be '19:05:45' and times like '12:15:00AM' must be '00:15:00'. – RobG May 19 '18 at 13:13
0

function to24HrFormat(s) {
  var z = n => (n<10?'0':'')+n;
  var b = s.match(/\d+/g);
  var ap = /am$/i.test(s)? 0 : 12;
  return z((b[0]%12) + ap) + ':' + b[1] + ':' + b[2];
}

// Tests
['07:05:45PM', '06:23:49AM', '12:15:00AM', '11:59:59pm']
  .forEach(s => console.log(s + ' => ' + to24HrFormat(s)));
O parkor
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 08 '23 at 06:32
-2

Try using Date.parse(string) instead. See referene.

kejn
  • 112
  • 6
  • Then i give NAN –  May 18 '18 at 07:13
  • 1
    Parsing of strings by the Date constructor and Date.parse is [*specified*](http://ecma-international.org/ecma-262/8.0/#sec-date-value) as being the same, the only difference is that *Date.parse* returns a time value (which might be NaN) whereas *new Date* returns a Date constructed from the time value returned by *Date.parse*. – RobG May 19 '18 at 09:02