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.
Related
This question already has an answer here:
Convert UTC date received from server to local timzone date in Javascript
(1 answer)
Closed last year.
I want to convert 2022-01-15T12:22:24.0078237 into Sat Jan 15 2022 23:24:18 GMT+1100. The answers in Convert UTC date time to local date time say that the Date constructor should convert from UTC to local date, but clearly this isn't working here. What can I do to convert the UTC date into the local client date?
new Date("2022-01-15T12:22:24.0078237")
> Sat Jan 15 2022 12:22:24 GMT+1100 (Australian Eastern Daylight Time)
new Date()
> Sat Jan 15 2022 23:24:18 GMT+1100 (Australian Eastern Daylight Time)
Add 'Z' to the date string, like this:
const dateString = "2022-01-15T12:22:24.0078237";
const localDate = new Date(dateString + "Z");
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);
Is there a simple way to convert standard Javascript date format to xs:dateTime
So I have a date value (new Date()) and I need in the format: 2015-01-16T20:26:53.974+03:00
so
Fri Jan 16 2015 22:26:53 GMT+0500 (Ekaterinburg Standard Time) ->
2015-01-16T20:26:53.974+03:00
It strange but cound't find simple solution.
I believe this is the same as the ISO date format
var date = new Date();
var formatted = date.toISOString();
When I retrieve the date value from c# it will be in the following format:
/Date(1406844000000)/
then in JavaScript i make this code :
var date1 = new Date(parseInt(ret.startDate.substr(6)));
and when I display this value in label it display as follow:
Fri Aug 01 2014 00:00:00 GMT+0200 (Egypt Standard Time)
and I need only
Fri Aug 01 2014
How extract only date in java script from above value?
You can convert the date to a string.
date1.toString().substr(0, 15)
date1.toDateString()
or if you want it localized:
date1.toLocaleDateString()
I'm getting incorrect dates in Chrome...
My code looks like this..
Title contains "2013-06-14T00:00:00", it was a DateTime in C# returned from WebAPI
As you can see here on both browsers..
When I add it to a new javascript date like this..
var dt = new Date(title)
I get different dates in different browsers...
Example - http://jsfiddle.net/RvUSq/
Looks like Firefox is assuming this datetime format without timezone is local time and Chrome/Webkit is assuming it's UTC.
If the datetime returned from the api is UTC, simply append a "Z" to the end of the string, so it becomes "2013-06-14T00:00:00Z", which indicates the time is in UTC, then you will get the same result in the two browsers.
Convert timestamp to ISO 8601 formatted string in C#, for e.g
var title = "14 JUN 2013 00:00:00" // printed from C#
Then use Date constructor
var date = new Date(title);
If you don't specify timezone the local timezone in the client machine will be set to the given time. If you specify the timezone, needed calculations will be done to convert the date to local timezone.
var title = "14 JUN 2013 00:00:00";
var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT";
var date = new Date(title); // Fri Jun 14 2013 05:30:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT-0400";
var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT+0530 (IST)
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse