How to change date format in HTML5

627 views Asked by At

I need to display a date in HTML(application is using HTML5+Thymeleaf).

How to convert from the below format

Wed Jul 01 09:09:51 IST 2015

To

Wed Jul 01 2015 09:09:51 GMT+0530 (Indian Standard Time)

And also , if I give new Date() in controller class, its displaying(in log) as

Thu Jun 11 06:41:11 IST 2015

But if I give new Date() in HTML, it is displaying(in alert) in below format.

Thu Jun 11 2015 06:41:12 GMT +0530 (Indian Standard Time)

HTML Code:

$('#validFrom').datepicker('setDate', new Date());
alert('validFrom ...'+new Date());

Can anyone help me in converting and to set date in HTML. Also it will be great if anyone can answer the difference of setting new Date() in controller class and HTML.

2

There are 2 answers

1
aadarshsg On

You will find this useful. How to Convert JavaScript Date to Date in Java?

Basically, Java and Javascript date formats are slightly different and you need converters and parsers.

1
Java_User On

In Controller:

model.addAttribute("validFromDate", validDate);

In HTML:

var validFromDate = [[${validFromDate}]];
$('#validFrom').datepicker('setDate', new Date(validFromDate));