How to remove seconds from time in javascript?

In a ListView you can use the TEXT function in a Calculated Column;

see https://www.365csi.nl/vm365com/365coach/#/Calculated_Column_Functions_List

TEXT ([DateTimeField],'dd-mm-yy hh:mm')

In a DisplayForm or Search results it is a minor javascript change to the DisplayTemplate

SharePoint adds a String.format
you can use/mix all these markers to format a Date value

String.format("{0:i}",new Date());  outputs: Wed Oct 07 2015 20:39:54 GMT+0200 (W. Europe Daylight Time)
String.format("{0:F}",new Date());  outputs: Wednesday, 07 October 2015 20:39:54
String.format("{0:f}",new Date());  outputs: Wednesday, 07 October 2015 20:39
String.format("{0:D}",new Date());  outputs: Wednesday, 07 October 2015
String.format("{0:s}",new Date());  outputs: 2015-10-07T20:39:54
String.format("{0:d}",new Date());  outputs: 10/07/2015
String.format("{0:dd}",new Date());  outputs: 07
String.format("{0:ddd}",new Date());  outputs: Wed
String.format("{0:dddd}",new Date());  outputs: Wednesday
String.format("{0:m}",new Date());  outputs: October 07
String.format("{0:M}",new Date());  outputs: October 07
String.format("{0:MM}",new Date());  outputs: 10
String.format("{0:MMM}",new Date());  outputs: Oct
String.format("{0:MMMM}",new Date());  outputs: October
String.format("{0:Y}",new Date());  outputs: 2015 October
String.format("{0:y}",new Date());  outputs: 2015 October
String.format("{0:yy}",new Date());  outputs: 15
String.format("{0:yyyy}",new Date());  outputs: 2015
String.format("{0:gg}",new Date());  outputs: A.D.
String.format("{0:T}",new Date());  outputs: 20:39:54
String.format("{0:t}",new Date());  outputs: 20:39
String.format("{0:HH}",new Date());  outputs: 20
String.format("{0:mm}",new Date());  outputs: 39
String.format("{0:ss}",new Date());  outputs: 54
String.format("{0:tt}",new Date());  outputs: AM or PM

J1 J5

Description

Remove seconds input=time

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">
body, input {
   margin:11px;
}
</style> <!--from w ww . ja  v  a  2s .c om-->
 </head> 
 <body> 
  <input type="date" value=""> 
  <input type="time" value="23:20:50.52"> 
  <!-- valid --> 
  <input type="time" value="17:39:57"> 
  <!-- valid --> 
  <input type="time" value="13:37"> 
  <input type="time" value=""> 
  <!--
http://dev.w3.org/html5/markup/input.time.html
http://dev.w3.org/html5/markup/input.time.html#input.time.attrs.value
http://dev.w3.org/html5/markup/datatypes.html#form.data.time
-->  
 </body>
</html>
  • Previous
  • Next
  • Limit an input characters to just number
  • Make Bootstrap input group full width
  • Multiple number inputs with different widths
  • Style input element to fill remaining width of its container
  • Twitter bootstrap input prepend full width

  • Remove From My Forums

  • Question

  • the following regular expression is supposed to remove the "seconds" component.  it works in Firefox and chrome but not IE 11 or Edge.

    var retval = new Date().toLocaleTimeString();
    retval = retval.replace(/(\d{1,2}:\d{2}):\d{2}/, "$1");

    // this is how it should convert

    "‎9‎:44‎:24‎ ‎AM" => "‎9‎:44‎ ‎AM"

    • Edited by Tuesday, April 26, 2016 2:47 PM

Answers

  • Hi Remo,

    >>"it works in Firefox and chrome but not IE 11 or Edge."

    In IE, toLocaleTimeString method will generate lots of invisible characters between : character.

    var retval = new Date().toLocaleTimeString();
    alert(retval.length);//It return 18 in IE and 11 in Chrome

    Following code should work in IE.

    retval = retval.replace(/(\d{1,2}\W:\W\d{2})\W:\W\d{2}/, "$1");

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Remo Imparato Thursday, April 28, 2016 2:17 PM

How to remove seconds in JavaScript?

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date. setSeconds(0, 0) .

How do I remove seconds from DateTime?

Solution 1 Using different methods on the DateTime, like . Today give the date part, but the other parts of it are zero. This is by design. If you don't want to display the seconds when you create the string, use a format string, like MM/dd/yyyy hh.mm and leave the tt part off.

How do I use toLocaleTimeString () without displaying seconds?

To use the toLocaleTimeString() method without displaying seconds, set the hour and minute parameters in the method's options object, e.g. date. toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'}) .

How do I remove seconds from a time in Python?

This article focuses on how to remove seconds on Python datetime objects. The seconds can be removed in two ways – simply truncating seconds by turning them to 0 or by rounding off seconds into minutes.