Matching Javascript Date() with date with Java new Date() - javascript

We have a Java new Date(); which returns the date as below:
Mon Feb 19 19:51:21 IST 2018
And the javascript Date() function returns the date in a format like below:
Mon Feb 19 2018 20:31:37 GMT+0530 (India Standard Time)
I have a rest webservice which accepts the date in exact format as that of Java's date (Mon Feb 19 19:51:21 IST 2018).
In an ajax request, I am passing an xml content which has a date tag along with other xml tags like id and author. Example below. If I send an xml like below, the date field gets ignored as the Date format expected in the rest webservice is different than the one below.
<message>
<autor>User2</autor>
<date>Mon Feb 19 2018 20:02:03 GMT+0530 (India Standard Time)</date>
<id>1</id>
<message>Message2</message>
</message>

Use .getTime() to get date value in milliseconds in JS like this:
var date = new Date().getTime();
And then on your Java back-end use value in milliseconds from JS to create date object:
Date date = new Date(dateFromJS);
And you can also add some formating to it:
String formatted = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

Related

How to convert local date (without time) in UTC DateTime in JavaScript

I am trying to convert the local date (without time) in UTC Date and pass it to server. Suppose I have a local date selected from Calendar which is 01-Jun-2021 and when converted to Date its Tue Jun 01 2021 00:00:00 GMT+0530.
My requirement is that, it should be converted to Mon May 31 2021 18:30:00 GMT+0530 and when sent to the server it should be Mon May 31 2021 18:30:00 only. I tried do that with below code and it give me the desired result. However when it is sent to the server its like 5/31/2021 1:00:00 PM in C#. My local develop machine is in IST Zone.
export const dateToUTC = (date: Date): Date => {
let _date = new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds()
);
return _date;
};
Can anyone please help me this query? I think the main issue is GMT+0530 which is being passed to server. I can do some tweaks and paly with the offset value but it might not work always.
For your case I would suggest using moment.js library.
The code would look like this:
// Moment wrapper object
var m = moment.parseZone('2021-06-01T00:00:00+05:30').utc();
// Default format
console.log(m.format()); // "2021-05-31T18:30:00Z"
// Your string for the server
console.log(m.format("ddd MMMM DD YYYY HH:mm:ss")); // Mon May 31 2021 18:30:00
JSFiddle
Indeed there is some tweaking underneath, but at least you can rely on it.

How to convert JSON time stamp in javascript to date and time?

I am getting Time stamp as "2020-03-02T05:50:31.000Z"
How to convert this to Normal Readable Format with Date and Time Zone
const parsedDate = new Date("2020-03-02T05:50:31.000Z");
console.log(parsedDate.toGMTString())
//"Mon, 02 Mar 2020 05:50:31 GMT"
console.log(parsedDate.toLocaleString())
//"3/2/2020, 11:20:31 AM"
console.log(parsedDate.toDateString(), parsedDate.toTimeString())
//Mon Mar 02 2020 11:20:31 GMT+0530 (India Standard Time)
In java-script above date format can be parsed by using Date.
Eg:
var date = new Date('2020-03-02T05:50:31.000Z');
I don't know what you mean readable format but you can use following methods:
new Date('2020-03-02T05:50:31.000Z').toLocaleString();
// outputs date according to user locale settings
Or you can use getYear, getMonth, getDay methods to get them and format date as you want
You can use moment.js for date time format and conversion.
Pass your date timestamp as below:
moment.parseZone("2020-03-02T05:50:31.000Z").format("DD-MM-YYYY hh:mm:ss z Z")
Modify format as you need ref mentioned here
Use in your code as mentioned here

PHP Datetime Object to Javascript Datetime Object

How do I convert PHP DateTime object to Javascript Date object ?
new Date('06/18/2016') in Javascript = Date {Sat Jun 18 2016 00:00:00 GMT+0800 (SE Asia Standard Time)}
It's easy to get month, just mydate.getMonth() but it didn't work when I try to convert date from PHP
new DateTime('06/18/2016') in PHP = Object { date="2016-06-18 00:00:00", timezone_type=3, timezone="Asia/Kuala_Lumpur"} using mydate.getMonth() didn't work.

Why does moment.js return this date wrong?

I'm simply trying to take an input string and convert it to a date object.
moment.utc('2000-01-01T00:00:00.000Z').toDate()
But it returns this...
Fri Dec 31 1999 19:00:00 GMT-0500 (EST)
Thanks
That is a valid JavaScript Date Object. You can test this by opening a console in Chrome or Firefox, then entering the following:
// Mon Nov 24 2014 09:54:00 GMT-0800 (PST) (looks the same as your example)
console.log( new Date() );
If you want to format the value coming out of moment.js, you can use its format method and a mask.
// Example: November 24th 2014, 09:58:12am
var fdate = moment().format('MMMM Do YYYY, h:mm:ss a');
Moment.js doesn't modify the prototype, it simply wraps it.
If you want to convert a string to a date object using moment.js, you can just call it as such:
moment(your_date); // Unless in UTC mode this will display as local time
In your instance you're using the UTC mode.
2000-01-01T00:00:00.000Z is the GMT date/time.
Using moment.utc("2000-01-01T00:00:00.000Z").toDate() returns this date/time according to your timzone settings.
See : http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/
Hope it helps.

Date format issue in javascript

I am trying to format a date from javascript as follows.
var startDate = new Date('Fri Oct 12 2012 10:15:00 GMT+0530 (India Standard Time)');
var dateString = startDate.format("ddd MMM dd yyyy"));
var timeString = startDate.format("h:mm tt"));
When running this from my machine i got both date and time string as expected. like
dateString as Fri Oct 12 2012 and timeString as 10:15 AM.
But in live server i am getting dateString as Fri 1515 12 2012 and timeString as 10:1010 AM. I think the issue is with format of Month and Minute.
The local server is here in India and the live server is in Us. So i am not sure, is it related with the date format in different culture?
What is the best way to format the date string from javascript. Is i am doing the right thing to format date string.
By using startDate.getDate() & startDate.getDay() & startDate.getFullYear()
view this http://www.w3schools.com/jsref/jsref_obj_date.asp it helps you.By using this date object properties you can get your appropriate result

Categories