How to format date with hours, minutes and seconds when using jQuery UI Datepicker?

JavascriptJqueryDatepickerJquery Ui-Datepicker

Javascript Problem Overview


Is it possible to format a date with jQuery UI Datepicker as to show hours, minutes and seconds?

This is my current mockup:

$(function() {
    $('#datepicker').datepicker({ dateFormat: 'yyy-dd-mm HH:MM:ss' }).val();
});

<html>
<head>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
  <title>snippet</title>
</head>
<body>

  <input type="text" id="datepicker">

  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

When I call .datepicker({ dateFormat: 'yyy-dd-mm HH:MM:ss' }) the returned value is: > 201313-07-03 HH:July:ss

Here is a JSFiddle.

Javascript Solutions


Solution 1 - Javascript

Try this fiddle

$(function() {
	$('#datepicker').datepicker({
		dateFormat: 'yy-dd-mm',
		onSelect: function(datetext) {
			var d = new Date(); // for now

			var h = d.getHours();
			h = (h < 10) ? ("0" + h) : h ;

			var m = d.getMinutes();
			m = (m < 10) ? ("0" + m) : m ;

			var s = d.getSeconds();
			s = (s < 10) ? ("0" + s) : s ;

			datetext = datetext + " " + h + ":" + m + ":" + s;

			$('#datepicker').val(datetext);
		}
	});
});

Solution 2 - Javascript

$("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd ");

For the time picker, you should add timepicker to Datepicker, and it would be formatted with one equivalent command.

EDIT

Use this one that extend jQuery UI Datepicker. You can pick up both date and time.

http://trentrichardson.com/examples/timepicker/

Solution 3 - Javascript

Using jQuery UI in combination with the excellent datetimepicker plugin from http://trentrichardson.com/examples/timepicker/

You can specify the dateFormat and timeFormat

$('#datepicker').datetimepicker({
	dateFormat: "yy-mm-dd",
	timeFormat:  "hh:mm:ss"
});

Solution 4 - Javascript

Solution 5 - Javascript

If don't like to add time picker, we can use just javascript for time part.

  var dateObj=new Date();
  var date = $.datepicker.formatDate('dd M yy', dateObj);
  var time  = dateObj.getHours()+":"+dateObj.getMinutes()+":"+dateObj.getSeconds(); 

  console.log(date," ",time);

Solution 6 - Javascript

I added this code

<input class="form-control input-small hasDatepicker" id="datepicker6" name="expire_date" type="text" value="2018-03-17 00:00:00">

<script src="/assets/js/datepicker/bootstrap-datepicker.js"></script>
<script>
        $(document).ready(function() {
            $("#datepicker6").datepicker({
                isRTL: true,
                dateFormat: "yy/mm/dd 23:59:59",
                changeMonth: true,
                changeYear: true

            });
        });
</script>

Solution 7 - Javascript

format: 'YYYY-MM-DD HH:mm:ss',

Solution 8 - Javascript

$(function() {
    $('#datepicker').datepicker({ dateFormat: 'yy-d-m ' }).val();
});

<html>
<head>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
  <title>snippet</title>
</head>
<body>

  <input type="text" id="datepicker">

  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

Solution 9 - Javascript

Getting Started Install from npm:

npm install imask And import or require:

import IMask from 'imask';

or use CDN:

Solution 10 - Javascript

This worked fine for me:

        $('#myelement').datetimepicker({
            dateFormat: "yy-mm-dd",
            timeFormat:  "hh:mm:ss"
        });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNETCreator Hosting - WebDesignView Question on Stackoverflow
Solution 1 - JavascriptArun UnnikrishnanView Answer on Stackoverflow
Solution 2 - Javascriptscel.piView Answer on Stackoverflow
Solution 3 - JavascriptMiguel StevensView Answer on Stackoverflow
Solution 4 - JavascriptCray KaoView Answer on Stackoverflow
Solution 5 - JavascriptvusanView Answer on Stackoverflow
Solution 6 - Javascriptمهدی عابدی برنامه نویس و مشاورView Answer on Stackoverflow
Solution 7 - JavascriptSalamView Answer on Stackoverflow
Solution 8 - JavascriptDip GhoshView Answer on Stackoverflow
Solution 9 - JavascriptArdhika RafiView Answer on Stackoverflow
Solution 10 - JavascriptNoleView Answer on Stackoverflow