4

I am using a HTML5 datetime-local picker, is there anyway I can place a custom placeholder text?

HTML Code:

<input id="dt" onchange="dateTimeFormat()" class="input" type="datetime-local">

By default it looks like this:
enter image description here

Theres not many resources into styling this, so any help is much appreciated!

TIA

Ayush Lal
  • 77
  • 1
  • 8
  • Your question is same like [How to add placeholder datetime-local field](https://stackoverflow.com/questions/30048323/how-to-add-placeholder-input-type-datetime-local-field) – Winky Aug 10 '20 at 03:58

3 Answers3

1

Yess You can use this:-

<input  id="dt"  class="input"  onfocus="(this.type='date')" placeholder="hello">

use onfocus="(this.type='date')" and then you can use placeholder as per you wise

Or

You can use this CSS trick to do this:-

input[type="date"]:before {
    content: attr(placeholder) !important;
    color: #aaa;
    margin-right: 0.5em;
  }
  input[type="date"]:focus:before,
  input[type="date"]:valid:before {
    content: "";
  }

after attaching this to your input date, then you can use the placeholder with the dates

For example:- this is an HTML code with the above-mentioned CSS code:-

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    input[type="date"]:before {
    content: attr(placeholder) !important;
    color: #aaa;
    margin-right: 0.5em;
  }
  input[type="date"]:focus:before,
  input[type="date"]:valid:before {
    content: "";
  }
  </style>
</head>
<body>
  <input type="date" name="date" id="date" placeholder="Hello">
</body>
</html>
1

I think might solve your problem

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
  </script>
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
 
 
</body>
</html>
0

For React, you can do this.

This was originally removed by a moderator as a duplicate. But it is not. Also, the question itself isn't flagged as a duplicate either. This answer is specific to React users. Also, the onBlur is required for proper functionality.

const onDateFocus = (e) => (e.target.type = "datetime-local");
const onDateBlur = (e) => (e.target.type = "text");
.
.
.
  <input
    onFocus={onDateFocus}
    onBlur={onDateBlur}
    type="text"
    placeholder="Event Date"
  />
Adrian Bartholomew
  • 2,506
  • 6
  • 29
  • 37