Nishant Rambhojun
Nishant Rambhojun

Reputation: 77

Formatting Date

I am on MVC.

In my model i got a variable Datetime

In my view I am trying to give this a format when the page loads.

Control

 @Html.TextBoxFor(m => m.RequestedLaunchDate, new { @class = "dateselector", @style = "float:left;" })

my JS is like

            var now = new Date();//Today's date
            now.format("dd/MM/yyyy");
            $(".dateselector").datepicker("setDate", now);

But still when the page loads i get the display as

18/03/2014 00:00:00

Why ?

What did I do wrong ?

Upvotes: 0

Views: 129

Answers (5)

Akshay Mohite
Akshay Mohite

Reputation: 109

var now = new Date();//Today's date
var day = now.getDate();
var month = now.getMonth()+1;
var year = now.getFullYear();
if (month < 10) { month = '0' + month; }
var newDate=(day + '/' + month + '/' + year); // This will give "dd/mm/yyyy"

$(".dateselector").datepicker("setDate", newDate);

Upvotes: 0

Nishant Rambhojun
Nishant Rambhojun

Reputation: 77

  var now = new Date();//Today's date 
  $(".dateselector").datepicker("setDate", now);

Solved my problem.

Upvotes: 0

Zafar
Zafar

Reputation: 3434

Date object in JavaScript has no format method by default. You might use some plugin such as moment.js.

However, you can do that without any JavaScript, but using the following data annotation on server side:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]

Put this line of code to right above the RequestedLaunchDate property of your Model

Or you can refer to this post for more options about formatting date with JavaScript.

Upvotes: 0

Emil L
Emil L

Reputation: 21071

Date in javascript has no format method defined (by default). What you probably want is to pass the formatting to jQuery UI datepicker as documented here. Something along the lines of:

var now = new Date();//Today's date
$(".dateselector").datepicker( "option", "dateFormat", "dd/MM/yyyy" );
$(".dateselector").datepicker("setDate", now);

This will set change how the datepicker formats your date. If you add it somewhere else on your page you can use:

var dateString = $.datepicker.formatDate( "dd/MM/yyyy", date );

This will give you a string representation of the date formatted as you wish.

Another, more robust way, would be to set the locale you want:

var locale = 'de'; // Use the locale you want.
$.datepicker.setDefaults( $.datepicker.regional[ locale ] );

Setting this will localize your datepicker along with the date format displayed in it.

Upvotes: 2

uma
uma

Reputation: 2952

please try to this solution to formate date.

It will help you:

var format_date = function(date){
    return  ("0"+date).slice(-2)
}
var format_month = function(month){
   return  ("0"+month).slice(-2)
}
var now = new Date();

formate_complete_date = format_date(now.getDate())+"/"+format_month(now.getMonth())+"/"+now.getFullYear() +" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();

console.log(formate_complete_date)

Upvotes: -1

Related Questions