-2

I have a time string which is 01:00:00 all I want to do is convert it to 1:00 am using Javascript date time conversion.

I have tried SimpleDateFormat which gives SimpleDateFormat is not defined

Following are the links i found but could not understand or implement:

  1. This gives Invalid Date

  2. This gives SimpleDateFormat is not defined

  3. This gives the same.

I just want to understand how date and time formatting works in Javascript.

I already do it in java with SimpleDateFormat, but i think it is not as easy in JS.

Please help. Thanks in Advance.

EDIT:

This link was suggested as a possible duplicate, but then the function in that answer accepts Date object. I want to convert string time to Date.

Varun Barve
  • 323
  • 2
  • 8
  • 18
  • are you aware that JS !== Java???? – ΦXocę 웃 Пepeúpa ツ Aug 09 '17 at 14:11
  • @ΦXocę웃Пepeúpaツ im fairly new to JS – Varun Barve Aug 09 '17 at 14:12
  • yeap... no problem... your question is related to js and not with java.... – ΦXocę 웃 Пepeúpa ツ Aug 09 '17 at 14:13
  • you are getting the attention of the wrong group of developers.... – ΦXocę 웃 Пepeúpa ツ Aug 09 '17 at 14:14
  • Java and JavaScript are different languages. Java solutions do not work in JavaScript. About point 2: You probably used the wrong name, because that JavaScript function is named `simpleDateFormat` and not `SimpleDateFormat` (note the difference, case is important). – Jesper Aug 09 '17 at 14:14
  • Possible duplicate of [How do you display javascript datetime in 12 hour AM/PM format?](https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format) –  Aug 09 '17 at 14:17
  • @Jesper still gives `simpleDateFormat is not defined` – Varun Barve Aug 09 '17 at 14:18
  • @Hugo and for the answer in that question to work, wouldn't i need to send the date object to the function ?! – Varun Barve Aug 09 '17 at 14:19
  • Did you actually include [the code from that page](https://github.com/noahcooper/SimpleDateFormatJS/blob/master/src/SimpleDateFormat.js) into your project? It's not a function that is available by default in JavaScript. – Jesper Aug 09 '17 at 14:20
  • You can also split the string `01:00:00` and get the values you want, and build the output in a similar way. As you care only about the time, you don't need to create a date object. –  Aug 09 '17 at 14:22
  • @Hugo i thought of doing that, but then i also want to learn and understand how date time works in JS. Also then this isn't a duplicate question right ?! – Varun Barve Aug 09 '17 at 14:24
  • I still think it's a duplicate, because it's **essentially** the same problem: how to format a time to am/pm format. If you also want to learn how date/time works in some language, that would be a too broad question for SO (in this case, you should search for books/tutorials/etc). If you [edit] the question adding your code attempts and explaining why it's different from another questions you've seen, maybe it won't be considered a duplicate (you just linked other questions but didn't explain what/how you've tried to solve your problem). –  Aug 09 '17 at 14:32
  • Case 1 doesn't work because `new Date` expects the full date in [some specific formats](https://www.w3schools.com/js/js_dates.asp). `simpleDateFormat` doesn't work because probably you're not including it in your project, or you're not calling it with lowercase `s` (`SimpleDateFormat` with uppercase `S` is a java class, not the javascript library). And for this specific problem, you don't need to create a date, you can just split the string, get the values for hour and minute, and use the code in the duplicate question in a similar way. –  Aug 09 '17 at 14:37
  • @Hugo Yaa... i think i'm left with no other options than to split it. – Varun Barve Aug 09 '17 at 14:42

1 Answers1

1

Try this , just use date object and extract hours and minutes , i think is easy to understand it

vvar str = "01:00:00"; var res = str.split(":"); 
document.getElementById("demo").innerHTML = res[0]; // =01 
document.getElementById("demo").innerHTML = res[1]; // =00 
document.getElementById("demo").innerHTML = res[2]; // =00 

so in your case , should be :

 var str = "01:00:00"; 
 var res = str.split(":");
 var hour = res[0]; // =01 
var minutes=res[1]; // = 00
var ampm = hour >= 12 ? 'PM' : 'AM';
hour = hour % 12;
hour = hour ? hour : 12; // the hour '0' should be '12'
minutes = minutes < 10 ?  minutes : minutes;
var strTime = hour + ':' + minutes + ' ' + ampm;
alert(strTime);

ps. if u wanna use java , you can do it in a jsp page where you can use java , html and javascript .

  • this is same as [link](https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format) right ?! But then it would need a date object right ?! – Varun Barve Aug 09 '17 at 14:27
  • yup , "var date = new Date()" is alredy a date object , u dont have to call any libraries – gian quiroz Aug 09 '17 at 14:33
  • But then how will i change `01:00:00` to the date object ?! – Varun Barve Aug 09 '17 at 14:33
  • oh , didn't read that , well is easy to solve , just use "split" function like this: `var str = "01:00:00"; var res = str.split(":"); document.getElementById("demo").innerHTML = res[0]; // =01 document.getElementById("demo").innerHTML = res[1]; // =00 document.getElementById("demo").innerHTML = res[2]; // =00 ` so in your case , should be : `var str = "01:00:00"; var res = str.split(":"); var hour = res[0]; // =01 var minutes=res[1]; // = 00 ` – gian quiroz Aug 09 '17 at 14:37
  • @gianquiroz You can [edit your answer](https://stackoverflow.com/posts/45593357/edit) and add this code to it (much better and more readable than putting in the comments) –  Aug 09 '17 at 14:41
  • thank for the suggestion , i'm new in stackoverflow , i'm sorry u.u – gian quiroz Aug 09 '17 at 14:45