If you only want the date portion, use type="date":
<input type="date" name="book_date" id="book_date"
onchange="console.log(this.value);" />
If you really want to use type="datetime-local", you can simply split the value by T:
<input type="datetime-local" name="book_date" id="book_date"
onchange="console.log(this.value.split('T')[0]);" />
Answer from Racil Hilan on Stack OverflowIf you only want the date portion, use type="date":
<input type="date" name="book_date" id="book_date"
onchange="console.log(this.value);" />
If you really want to use type="datetime-local", you can simply split the value by T:
<input type="datetime-local" name="book_date" id="book_date"
onchange="console.log(this.value.split('T')[0]);" />
You're looking for date
<input id="datetime" type="date" name="book_date" id="book_date">
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types
c# - Display only date and no time - Stack Overflow
c# - ASP.NET MVC displaying date without time - Stack Overflow
html - How to display date as mm/dd/yyyy without time in React.js - Stack Overflow
css - Display the current date and time using HTML and Javascript with scrollable effects in hta application - Stack Overflow
Videos
Just had to deal with this scenario myself - found a really easy way to do this, simply annotate your property in the model like this:
[DataType(DataType.Date)]
public DateTime? SomeDateProperty { get; set; }
It will hide the time button from the date picker too.
Sorry if this answer is a little late ;)
If the column type is DateTime in SQL then it will store a time where you pass one or not.
It'd be better to save the date properly:
model.ReturnDate = DateTime.Now;
and then format it when you need to display it:
@Html.Label(Model.ReturnDate.ToShortDateString())
Or if you're using EditorFor:
@Html.EditorFor(model => model.ReturnDate.ToShortDateString())
or
@Html.EditorFor(model => model.ReturnDate.ToString("MM/dd/yyyy"))
To add a property to your model add this code:
public string ReturnDateForDisplay
{
get
{
return this.ReturnDate.ToString("d");
}
}
Then in your PartialView:
@Html.EditorFor(model => model.ReturnDateForDisplay)
EDIT:
I just want to clarify for this answer that by my saying 'If you're using EditorFor', that means you need to have an EditorFor template for the type of value you're trying to represent.
Editor templates are a cool way of managing repetitive controls in MVC:
http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/
You can use them for naive types like String as I've done above; but they're especially great for letting you template a set of input fields for a more complicated data type.
The problem you have here is that you are using a string value rather than a DateTime.
change your model to be:
[DataType(DataType.Date)]
[Display(Name = "Date of birth")]
public DateTime? DateOfBirth { get; set; }
DataType will only work if it's a DateTime type, you also get the added advantage that it will automatically validate it as a valid date when using a DateTime. If you use string, you will have to use a regular expression validator to ensure a proper date has been entered.
This should do it for edit mode and display
[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:MM/dd/yyyy}")]
though if it is just display, this might work
[DisplayFormat(DataFormatString = "{0:d}")]
a JS date will always contain the time data even if you didn't provide the time, for the display of the datepicker depends on the datepicker settings/configurations. or you can handle it manually on your side too.
if call.date is a string and contains ISO Date string, then you can do this to display date without time:
<td input type={Date}>{call.date.substring(0, 10)}</td>
or other ways:
// Parse into JS date object
const date = new Date(call.date)
...
<td input type={Date}>{date.toISOString().substring(0, 10)}</td>
You use normal javascript, or moment.js you could import
Vanilla javascript:
let today = new Date(); // get the date
let day = ("0" + today.getDate()).slice(-2); //get day with slice to have double digit day
let month = ("0" + (today.getMonth() + 1)).slice(-2); //get your zero in front of single month digits so you have 2 digit months
let date = month + '/' + day + '/' + today.getFullYear();
Then
<td input type={Date}>{date}</td>
Method 1:
With marquee tag.
HTML
<marquee behavior="scroll" bgcolor="yellow" loop="-1" width="30%">
<i>
<font color="blue">
Today's date is :
<strong>
<span id="time"></span>
</strong>
</font>
</i>
</marquee>
JS
var today = new Date();
document.getElementById('time').innerHTML=today;
Fiddle demo here
Method 2:
Without marquee tag and with CSS.
HTML
<p class="marquee">
<span id="dtText"></span>
</p>
CSS
.marquee {
width: 350px;
margin: 0 auto;
background:yellow;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
color:blue;
font-size:18px;
}
.marquee span {
display: inline-block;
padding-left: 100%;
text-indent: 0;
animation: marquee 15s linear infinite;
}
.marquee span:hover {
animation-play-state: paused
}
@keyframes marquee {
0% { transform: translate(0, 0); }
100% { transform: translate(-100%, 0); }
}
JS
var today = new Date();
document.getElementById('dtText').innerHTML=today;
Fiddle demo here
This will help you.
Javascript
debugger;
var today = new Date();
document.getElementById('date').innerHTML = today
Fiddle Demo
What is best practice when dealing with date values without time, in JavaScript? My concern is subtle off-by-one bugs caused by local Time Zone (TZ) offset (e.g. +5 hours), when doing date math.
JavaScript's built-in Date type represents a date and time not just the date. Internal representation is an integer of microseconds since 1970-01-01 00:00:00 UTC. Other languages, like Python, have separate Date and DateTime types. Java 8 introduced LocalDate.
You also have things like: new Date('5/18/2020') is local TZ (in US), but new Date('2022-05-18') is UTC. Same with Date.parse(string). And the time zone on most servers in UTC, whereas on the browser side the time zone will vary.
The date values will be used for simple date math in code and will be stored in a SQL database.
Possibilities:
-
Use the an alternate type like integer value, of milliseconds or days (since 1970-01-01), or string in
YYYYMMDDformat. -
This was combined with #1 -
Use
Dateignoring time (as 00:00 local TZ). Convert from/to UTC when reading/writing to database -
Use
Datewith time as 00:00 UTC. Have to be careful not to mix withDatevalues in local TZ (e.g.now = new Date()) -
Use
Datein local TZ, but convert to UTC when read/writing to database. This is a variant of #3. -
Create a
LocalDateclass that enforces midnight. -
Use a library. js-joda has
LocalDate.
I am leaning towards #3 and #6. Some code I am writing:
class LocalDate extends Date {
// Error if time isn't midnight local TZ
// Do not accept string in ISO format
constructor(date?: Date|number|string)
// Convert to 00:00 UTC
// To be used before write to database
toUtc(): Date
// Only return date. Also affects toJSON()
toISOString(): string
// Returns today's date at 00:00 Local TZ
static today(): LocalDate
// Set time to midnight local TZ, without error check.
static fromDateTime(date: Date|number): LocalDate
// Convert to local TZ. Error if not 00:00 UTC.
static fromUtc(date: Date|number): LocalDate
}UPDATE:
Various edits.
Here's one way. You have to get the individual components from the date object (day, month & year) and then build and format the string however you wish.
n = new Date();
y = n.getFullYear();
m = n.getMonth() + 1;
d = n.getDate();
document.getElementById("date").innerHTML = m + "/" + d + "/" + y;
<p id="date"></p>
Use Date::toLocaleDateString.
new Date().toLocaleDateString()
= "9/13/2015"
You don't need to set innerHTML, just by writing
<p>
<script> document.write(new Date().toLocaleDateString()); </script>
</p>
will work.

P.S.
new Date().toDateString()
= "Sun Sep 13 2015"
If you use DisplayFor, then you have to either define the format via the DisplayFormat attribute or use a custom display template. (A full list of preset DisplayFormatString's can be found here.)
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? AuditDate { get; set; }
Or create the view Views\Shared\DisplayTemplates\DateTime.cshtml:
@model DateTime?
@if (Model.HasValue)
{
@Model.Value.ToString("MM/dd/yyyy")
}
That will apply to all DateTimes, though, even ones where you're encoding the time as well. If you want it to apply only to date-only properties, then use Views\Shared\DisplayTemplates\Date.cshtml and the DataType attribute on your property:
[DataType(DataType.Date)]
public DateTime? AuditDate { get; set; }
The final option is to not use DisplayFor and instead render the property directly:
@if (Model.AuditDate.HasValue)
{
@Model.AuditDate.Value.ToString("MM/dd/yyyy")
}
I have been using this change in my code :
old code :
<td>
@Html.DisplayFor(modelItem => item.dataakt)
</td>
new :
<td>
@Convert.ToDateTime(item.dataakt).ToString("dd/MM/yyyy")
</td>