JSP and JavaScript convert date - javascript

I have a problem while trying to convert a specific type of date.
My goal is to get it into this format: dd/MM/yyyy
The current date format: Thu Apr 04 00:00:00 EEST 2013
When I alerted using JavaScript, it responds that it is not a date. I used many solutions like to format it on JSP:
<fmt:formatDate value="${theDate}" pattern="dd/MM/yyyy"/>
Result error:
Attempt to convert String "Thu Apr 04 00:00:00 EEST 2013" to type "java.util.Date", but there is no PropertyEditor for that type.
And even in Javascript:
var dateCreation = new Date(theDate);
The problem in JavaScript is that it says that dateCreation is not a date. Any ideas?

Problem with your date string "Thu Apr 04 00:00:00 EEST 2013"
when i tried with your date string got IllegelArgumentException
change "Thu Apr 04 00:00:00 EEST 2013" to "Thu Apr 04 00:00:00 EST 2013" then working fine.
I'm unable to guess why you got that EEST (one E extra).
public static void main(String[] args) {
Date date = new Date("Thu Apr 04 00:00:00 EST 2013");
System.out.println(date);
}
You are getting error in javascript because your passing the same string to JS i guess.

fmt:formatDate expects a java date object as value. In your case looks like theDate is not a date object but a String representation of date object. Please provide a date object and that should work.
Also in JavaScript what is the value of theDate param and where is it defined?

The date object in JavaScript has the following constructors:
var d = new Date();
var d = new Date(milliseconds); // from epoch
var d = new Date(dateString); // "yyyy-mm-dd hh:mm:ss"
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Make sure you use one of them. Bear in mind that the month is 0-based for the last mentioned constructor.

EST is GMT-05:00 while EEST is GMT+03:00. The difference occurs e.g. when parsing "Thu Apr 04 23:00:00 EEST 2013". Java (7) can parse EEST, but it can not format in EEST.
String s = "Thu Apr 04 23:00:00 EEST 2013";
SimpleDateFormat sdfParse = new SimpleDateFormat("E MMM d H:m:s z yyyy", Locale.ENGLISH);
Date d = sdfParse.parse(s);
SimpleTimeZone stz = new SimpleTimeZone(3 * 3600000 /* GMT+03:00 */, "EEST");
GregorianCalendar cal = new GregorianCalendar(stz, Locale.ENGLISH);
cal.setTime(d);
String ddmmyyyy = String.format("%02d/%02d/%4d", cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
System.out.println(s+"\n"+ddmmyyyy);
In JavaScript:
function format2(s) {s="0"+s;return s.substr(s.length-2);}
function dateFormat(theDate) {
//theDate = "Thu Apr 04 00:00:00 EEST 2013";
var timestamp = Date.parse(theDate.replace(/EEST/, "GMT+0300"));
var datePseudoGMT = new Date(timestamp+3 * 3600000);
var dateCreation = format2(datePseudoGMT.getUTCDate())
+"/"+format2(datePseudoGMT.getUTCMonth()+1)
+"/"+format2(datePseudoGMT.getUTCFullYear());
console.log(dateCreation);
return dateCreation;
}

Related

How to create function that return a Date object?

1 - Make a function that given a date in text format "dd/mm/yyyy" returns a Date object with that date, using the Split() method.
2 - You have to do a "console.log() of the Date object created" and it should output something similar to
Mon Dec 2 2019 11:36:25 GMT+0100 (Central European Standard Time).
I have tried this but I don't know why when I write a console.log
I get the following:
2022-12-05T00:00:00.000Z
And I try to get it to appear something like this:
Mon Dec 2 2019 12:30:05 GMT+0100 (Central European Standard Time)
function convertDate(date) {
let dateArray = date.split("/");
let DateString = dateArray[2] + "/" + dateArray[1] + "/" + dateArray[0] + "Z";
let dateDate = new Date(dateString);
return dateDate;
}
console.log(convertDate("05/12/2022"));
you can use new Date() to get the date like "Wed Dec 28 2022 11:36:25 GMT+0100"
let date = new Date("05/12/2022");
console.log(date);
output will be Thu May 12 2022 00:00:00 GMT+0500 (Pakistan Standard Time) {}

How to convert string date-time range to javascript date objects?

I have a date-time range string that looks like this.
2019-06-14, 12:00:00 AM - 2019-06-15, 12:00:00 AM
2019-06-14, 12:00:00 AM is the start date.
2019-06-15, 12:00:00 AM is the end date.
I want to convert this string into two javascript date objects named "startdate" and "enddate".
How do i do that? I am comfortable using moment.js as well, so a solution with moment.js will also work for me.
Thanks!
Try this code below
function getStartAndEndDate( dateStr ){
let dates = dateStr.split( " - " );
let start = new Date( dates[0] );
let end = new Date( dates[1] );
return{ startDate: start, endDate: end };
}
Usage:
getStartAndEndDate( "2019-06-14, 12:00:00 AM - 2019-06-15, 12:00:00 AM" );
Output
{startDate: Fri Jun 14 2019 00:00:00 GMT-0500 (Central Daylight Time), endDate: Sat Jun 15 2019 00:00:00 GMT-0500 (Central Daylight Time)}
The best way to do this is using new Date()
Example:
var stringDate = '2019-06-14, 12:00:00 AM '
var startDate = new Date(stringDate)
console.log(startDate)
var stringDate2 = '2019-06-15, 12:00:00 AM'
var finishDate = new Date(stringDate2)
console.log(finishDate)

Easy way to convert a timestamp to a human readable date

I have the following timestamp: 2016-03-29T14:14:43.000Z. Is there any easy way to use JavaScript to make it look something like the following: Mar 29, 2016 2:14p? I tried using Date.parse() but it didn't seem to do anything.
{{yourValue| date:"MMM d, yyyy h:ma"}}
var ts = "2016-03-29T14:14:43.000Z";
var date = new Date(ts);
console.log(date); // Displays Tue Mar 29 2016 16:14:43 GMT+0200 (Romance Summer Time)
Is that what you need ?
var ds = date.toUTCString();
console.log(ds.substr(0,24)); // Displays Tue Mar 29 2016 16:14:43

Remove time from GMT time format

I am getting a date that comes in GMT format, Fri, 18 Oct 2013 11:38:23 GMT. The problem is that the time is messing up the timeline that I am using.
How can I strip out everything except for the actual date?
If you want to keep using Date and not String you could do this:
var d=new Date(); //your date object
console.log(new Date(d.setHours(0,0,0,0)));
-PS, you don't need a new Date object, it's just an example in case you want to log it to the console.
http://www.w3schools.com/jsref/jsref_sethours.asp
Like this:
var dateString = 'Mon Jan 12 00:00:00 GMT 2015';
dateString = new Date(dateString).toUTCString();
dateString = dateString.split(' ').slice(0, 4).join(' ');
console.log(dateString);
I'm using this workaround :
// d being your current date with wrong times
new Date(d.getFullYear(), d.getMonth(), d.getDate())
You could use Moment.js, a library that provides many helper functions to validate, manipulate, display and format dates and times in JavaScript.
Using Moment.js lib:
var dateString = new Date('Mon Jan 12 00:00:00 GMT 2015');
moment(dateString).format('YYYY-MM-DD HH:mm');
Or simplified:
moment('Mon Jan 12 00:00:00 GMT 2015').format('YYYY-MM-DD HH:mm')
Well,
Here is my Solution
let dateString = 'Mon May 25 01:07:00 GMT 2020';
let dateObj = new Date(dateString);
console.log(dateObj.toDateString());
// outputs Mon May 25 2020
See its documentation on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString
Just cut it with substring:
var str = 'Fri, 18 Oct 2013 11:38:23 GMT';
str = str.substring(0,tomorrow.toLocaleString().indexOf(':')-3);
In this case you can just manipulate your string without the use of a Date object.
var dateTime = 'Fri, 18 Oct 2013 11:38:23 GMT',
date = dateTime.split(' ', 4).join(' ');
document.body.appendChild(document.createTextNode(date));
You can first convert the date to String:
String dateString = String.valueOf(date);
Then apply substring to the String:
dateString.substring(4, 11) + dateString.substring(30);
You need to take care as converting date to String will actually change the date format as well.

Convert date to end of day

I get time in milliseconds from the server. I convert it to Date and get -
Mon Jul 22 2013 11:16:01 GMT+0200 (W. Europe Daylight Time) as the date in the record.
I want to separate out data of Monday, Tuesday etc into arrays. I am thinking of converting this date to Mon Jul 22 2013 23:59:59 GMT+0200 (W. Europe Daylight Time) and then filter out the records.
How can i change the date to the required end of the day time? or is there an easier way to do this ?
You could always construct a new DateTime object just using the year, month and day properties from the existing date, like so:
var actualDate = new Date(); // 2013-07-30 17:11:00
var endOfDayDate = new Date(actualDate.getFullYear()
,actualDate.getMonth()
,actualDate.getDate()
,23,59,59); // 2013-07-30 23:59:59
For future visitors, just use
var start = new Date();
var end = new Date();
start.setHours(0,0,0,0);
end.setHours(23,59,59,999);
Using http://momentjs.com:
var now = new Date().getTime();
var endOfDay = moment(now).endOf("day").toDate(); // Wed Jan 20 2016 23:59:59 GMT-0800 (PST)
var actualDate = new Date()
var eodDate = new Date(Math.floor(actualDate.getTime()/86400000+1)*86400000 + actualDate .getTimezoneOffset()*60000 - 1000)
where 86400000 are total milliseconds in a day
If two Date Objects are on the same day then they have the same Date String:
new Date('1374488161000').toDateString()
=> "Tue Jul 30 2013"
new Date('13744917610403').toDateString()
=> "Tue Jul 30 2013"
Although a rather naive method of comparing days, it's probably the simplest comparison.

Categories