I have a string "03/31/2017". I need to pass it as a Date to the SQL database. I tried to use new Date("03/31/2017") but it returns Thu Mar 30 2017 00:00:00 GMT-0400 (Eastern Daylight Time) Is there anyway I can keep the original date format as a data object without using momentum or anyother library. I can't use any library but jQuery.
The correct date format for Javascript is: YYYY-MM-DD
var dt = new Date('2017-03-27');
To be safer, do NOT use a string to specify the date, instead supply the parameters:
var dt = new Date(2017, 3 - 1, 27); // months are zero based
Related
I want to convert the returned value of new Date to UTC format using moment js.
I tried using moment.utc but
moment.utc(moment(new Date())).format();
output -> 2020-01-01T04:00:00Z
but I want output in this format
Thu Jun 04 2020 00:00:15 GMT+0530 (India Standard Time)
just like what new Date returns but it should be in UTC.
and the other problem is that the return type of format is of
TYPE : STRING I want that the format should be the same but its type should be of date object.
Any help would be appreciated.
In my javascript i want to convert date from date string.
i have string like
date = "Thu Sep 03 2015 19:30:00 GMT+0000"
Now i convert string using Date object.
var d = new Date(date);
But this gives me,
Fri Sep 04 2015 01:00:00 GMT+0530 (IST)
It automatically add one day into day. What is wrong?
It automatically add one day into day. What is wrong?
Nothing. The time you input is 19:30 GMT and the timezone on the device you're using is set to GMT+0530. Add 5 hours 30 minutes to 7:30pm and you get 01:00am the following day.
You should not use the Date constructor to parse strings, as it is inconsistent across browsers and until recently, entirely implementation dependent. Manually parse strings, or use a Date library.
I have a date in the following format
Fri Mar 16 2012 05:53:18 GMT 0200 (GTB Standard Time)
And I want to convert it into a unix timestamp.
Until now I manually split the string by spaces and then I am giving it as an input to a Date object, in order to get milliseconds in a latter step.
Is there any easiest way?
(I am trying to avoid jQuery plug-ins and do it using vanila javascript)
Yes, the easiest way would be to pass the string to Date object and then call the getTime method:
var myDate = new Date('Fri Mar 16 2012 05:53:18 GMT+0200 (GTB Standard Time)');
console.log( myDate.getTime() ); //1331869998000
No need to split your string by spaces.
I'm trying to convert a date string into a date object without changing the timezone. Here is the standard behavior:
new Date ("2014-10-24T00:00:00")
result
Thu Oct 23 2014 19:00:00 GMT-0500 (Central Daylight Time)
I am able to reverse the timezone by getting the offset in minutes, multiplying it by 60,000, and then adding that to the new string date.
new Date(new Date("2014-10-24T00:00:00").getTime() + new Date().getTimezoneOffset()*60000)
This works, but it seems like there must be a better way that doesn't require created three date objects.
Do not parse strings using the Date constructor. It calls Date.parse which, despite being standardised for one version of ISO 8601 strings in ES5, is still almost entirely implementation dependent.
I'm trying to convert a date string into a date object without changing the timezone.
> new Date ("2014-10-24T00:00:00")
That string will be treated differently in different browsers. If you want it to be treated as UTC, then it is simple to parse yourself:
function parseISOAsUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]||0)));
}
console.log(parseISOAsUTC('2014-10-24T00:00:00').toISOString()); // 2014-10-24T00:00:00.000Z
Now you can be certain that the string will be treated as UTC in all browsers in use (including the 20% or so still using IE 8 and lower).
If, on the other hand, you want the string to be treated as a local time, then just remove the Date.UTC part:
function parseISOAsLocal(s) {
var b = s.split(/\D/);
return new Date(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]||0));
}
console.log(parseISOAsLocal('2014-10-24T00:00:00')); // Fri 24 Oct 2014 00:00:00 <local timezone>
Here is an implementation of zerkms's solution.
new Date("2014-10-24T00:00:00".replace('T', ' '))
result
Fri Oct 24 2014 00:00:00 GMT-0500 (Central Daylight Time)
When I print out a date in javascript it adds GMT-0400 (EDT) to the end of it, is there a way I can cut this off? I'm using
date=Date()
document.write(date)
To get the date and time but I dont want the trailing GMT-0400 (EDT)
You should be able to just get the appropriate parts out of the date object:
var date = new Date();
[date.toDateString(), date.toLocaleTimeString()].join(' ');
// "Wed Oct 03 2012 12:13:56"
Use var date = new Date(); Date is a constructor and should be used with the new keyword.
You need to format it. The display, by default, is that full string.
You can use the Date API to build a string, or one of the various date formatting libraries, or even just manipulate it like a string (since it is):
var date = new Date();
document.write(date.split('-')[0]);
Also, you really shouldn't use document.write for a bunch of reasons, but I digress since your question was not about that.