Set date format javascript dd mm yyyy

I would like to add a current date to a hidden HTML tag so that it can be sent to the server:


How can I add a formatted date to the VALUE attribute?

gurghet

7,5514 gold badges36 silver badges61 bronze badges

asked Sep 13, 2012 at 15:04

4

I hope this is what you want:

const today = new Date[];
const yyyy = today.getFullYear[];
let mm = today.getMonth[] + 1; // Months start at 0!
let dd = today.getDate[];

if [dd < 10] dd = '0' + dd;
if [mm < 10] mm = '0' + mm;

const formattedToday = dd + '/' + mm + '/' + yyyy;

document.getElementById['DATE'].value = formattedToday;

How do I get the current date in JavaScript?

shareef

8,76413 gold badges58 silver badges88 bronze badges

answered Sep 13, 2012 at 15:06

AeliosAelios

11.4k2 gold badges35 silver badges54 bronze badges

18

I honestly suggest that you use moment.js. Just download moment.min.js and then use this snippet to get your date in whatever format you want:


$[document].ready[function[] {

     // set an element
     $["#date"].val[ moment[].format['MMM D, YYYY'] ];

     // set a variable
     var today = moment[].format['D MMM, YYYY'];

}];

Use following chart for date formats:

answered Apr 3, 2014 at 6:30

AliAli

4,8214 gold badges25 silver badges40 bronze badges

11


document.getElementById["date"].value = new Date[].toJSON[].slice[0,10]

answered Sep 29, 2013 at 14:53

Varun NatraajVarun Natraaj

7,1642 gold badges17 silver badges19 bronze badges

12

To get current date/time in javascript:

var date = new Date[];

If you need milliseconds for easy server-side interpretation use

var value = date.getTime[];

For formatting dates into a user readable string see this

Then just write to hidden field:

document.getElementById["DATE"].value = value;

answered Sep 13, 2012 at 15:13

WutzWutz

1,5019 silver badges12 bronze badges

2

By using the value attribute:

var today = new Date[];
document.getElementById['DATE'].value += today;

answered Sep 13, 2012 at 15:08

AsciiomAsciiom

9,7277 gold badges38 silver badges57 bronze badges

Use the DOM's getElementByid method:

document.getElementById["DATE"].value = "your date";

A date can be made with the Date class:

d = new Date[];

[Protip: install a javascript console such as in Chrome or Firefox' Firebug extension. It enables you to play with the DOM and Javascript]

answered Sep 13, 2012 at 15:07

Bart FriederichsBart Friederichs

32.2k15 gold badges96 silver badges186 bronze badges

You edit an element's value by editing it's .value property.

document.getElementById['DATE'].value = 'New Value';

answered Sep 13, 2012 at 15:06

gen_Ericgen_Eric

217k40 gold badges295 silver badges334 bronze badges

What date format is DD MMM YYYY JavaScript?

There is no native format in JavaScript for” dd-mmm-yyyy”. To get the date format “dd-mmm-yyyy”, we are going to use regular expression in JavaScript. The regular expression in JavaScript is used to find the pattern in a string. So we are going to find the “dd-mmm-yyyy” pattern in the string using match[] method.

How do you format a date mm dd yyyy?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.

How can I get date in dd mm yyyy format in typescript?

“get current date in typescript in mm/dd/yyyy format” Code Answer's.
var today = new Date[];.
var dd = String[today. getDate[]]. padStart[2, '0'];.
var mm = String[today. getMonth[] + 1]. padStart[2, '0']; //January is 0!.
var yyyy = today. getFullYear[];.
today = mm + '/' + dd + '/' + yyyy;.
document. write[today];.

How do I format a date in JavaScript?

The preferred Javascript date formats are: Dates Only — YYYY-MM-DD. Dates With Times — YYYY-MM-DDTHH:MM:SSZ..
MM — month from 01 to 12..
MMM — month abbreviation from Jan. to Dec..
DD — day from 01 to last day of the month [various].
YYYY — year as a 4-digit number..

Chủ Đề