How to Set Datetimepicker to Month and Year Only Format

How to set Datetimepicker to Month and Year only format?

Use DateTimerPicker.Format property. Check MSDN

public void SetMyCustomFormat()
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/yyyy";

A DateTimePicker to change only month and year

Here's a simple DateTimePicker custom control that pre-defines a custom DateTime format and modified the MonthCalendar view, configuring it show just the Months and the Year when is opened.

I've added a public property, ShowToday, that allows to specify whether the MonthCalendar popup should show the Today date at the bottom of the Calendar interface.

The custom control overrides OnDropDown to get the Handle of the MonthCalendar popup, sending a DTM_GETMONTHCAL message and changes its current view sending a MCM_SETCURRENTVIEW, specifying MCMV_YEAR as the value parameter.

This is how it look like:

DateTimePicker Custom Month Year only

using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class DateTimePickerYearMonth : DateTimePicker
public DateTimePickerYearMonth() {
this.CustomFormat = "MM-yyyy";
this.Format = DateTimePickerFormat.Custom;
this.Value = DateTime.Now;

Browsable(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(false),
Description("Shows or hides \"Today\" date at the bottom of the Calendar Control")
public bool ShowToday {
get => m_ShowToday;
set {
if (value != m_ShowToday) {
m_ShowToday = value;

protected override void OnHandleCreated(EventArgs e)

protected override void OnDropDown(EventArgs e)
var hWnd = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
if (hWnd != IntPtr.Zero) {
SendMessage(hWnd, MCM_SETCURRENTVIEW, 0, (int)MonCalView.MCMV_YEAR);

private void ShowMonCalToday(bool show)
int styles = SendMessage(this.Handle, DTM_GETMCSTYLE, 0, 0).ToInt32();
styles = show ? styles &~(int)MonCalStyles.MCS_NOTODAY : styles | (int)MonCalStyles.MCS_NOTODAY;
SendMessage(this.Handle, DTM_SETMCSTYLE, 0, styles);

static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private const int DTM_FIRST = 0x1000;
private const int DTM_GETMONTHCAL = DTM_FIRST + 8;
private const int DTM_SETMCSTYLE = DTM_FIRST + 11;
private const int DTM_GETMCSTYLE = DTM_FIRST + 12;
private const int MCM_FIRST = 0x1000;
private const int MCM_GETCURRENTVIEW = MCM_FIRST + 22;
private const int MCM_SETCURRENTVIEW = MCM_FIRST + 32;
private bool m_ShowToday = false;

public enum MonCalView : int

public enum MonCalStyles : int
MCS_DAYSTATE = 0x0001,
MCS_NOTODAY = 0x0010,

how can display only month and year in DateTimePicker control

You need three steps to modify display format of DateTimePicker:

  • Set Format property to DateTimePickerFormat.Custom
  • Set CustomFormat property to "MMMM yyyy"
  • Assign Value

Now assigned date will be displayed in format January 2013.

How to set default month and day in the datetimepicker?

You cannot create DateTime object only from day and month. DateTime simply doesn't have this kind of constructor. DateTime Constructors

So you need to go with some kind of "workaround"

- Use "dummy" year and when you need to use a date - use only Month and Day properties.

var dummyYear = 2000;
dateTimePicker2.Value = new DateTime(dummyYear, 12, 31);

Another workaround will be to use ParseExact method which will create DateTime based on the format you are using "dd/MM"

var date = DateTime.ParseExact("31/12", "dd/MM", CultureInfo.InvariantCulture); 
dateTimePicker2.Value = date; // 12/31/2017

Notice that when you did not provide a year - current year will be used.

Another notice: DD is invalid format for days it should be lower case "dd"

jQuery UI DatePicker to show month year only

Here's a hack (updated with entire .html file):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xml:lang="en" lang="en">
<script src=""></script>
<script type="text/javascript" src=""></script>
<link rel="stylesheet" type="text/css" media="screen" href="">
<script type="text/javascript">
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
.ui-datepicker-calendar {
display: none;
<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />

jsfiddle for the above example:

Adds the month year value to input box only on clicking of Done button.
Also allows to delete input box values, which isn't possible in above field

updated Better Solution based on rexwolf's solution down.

Bootstrap Date picker Display only month in charecter format and year

Change format in datepicker to format: "MMM-YYYY", it uses moment.js format.

format: "MMM-YYYY",
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down"

Boostrap Datepicker - months and year only doesn't work

The functionality you are looking for is in datetimepicker, not in just datepicker. It has the UI the way you want.

Note: The calendar will show Month or Year, but when you select a particular value, it will show it as YYYY-MM format. It is not possible to show either YYYY or MM in the textbox after selecting the value from calendar.

$(function() {  $('#datetimepicker2').datetimepicker({    viewMode: 'months',    format: 'YYYY-MM'  });});
<link rel="stylesheet" href="">
<!-- Optional theme --><link rel="stylesheet" href=""><link rel="stylesheet" href="" />
<script src=""></script><script src=""></script><script src=""></script>
<script src=""></script>
<div class="container"> <div class="row"> <div class='col-sm-6'> <div class="form-group"> <div class='input-group date'> <input type='text' class="form-control" id='datetimepicker2' /> </div> </div> </div> </div></div>

Bootstrap datePicker(Months and years only) wont show properly

You might want to add the following properties to your datepicker initialization:

startView: "months",
minViewMode: "months"

Working example:

$('#table').datepicker({  format: 'MM/yyyy',  icons: {    time: 'fa fa-time',    date: 'fa fa-calendar',    up: 'fa fa-chevron-up',    down: 'fa fa-chevron-down',    previous: 'fa fa-chevron-left',    next: 'fa fa-chevron-right',    today: 'fa fa-screenshot',    clear: 'fa fa-trash',    close: 'fa fa-remove'  },  startView: "months",  minViewMode: "months"});
<script src=""></script><script src=""></script><link href="" rel="stylesheet" /><link href="" rel="stylesheet" />
<script src=""></script><link href="" rel="stylesheet" /><div class='input-group date datepicker' id='table'> <input type='text' name="tgl" class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span></div>

Related Topics

Leave a reply