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.
Related
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
I am a newbie in javascript, come to my question.
I am using ionic 2 build application in which i am using date time picker for taking a time.
I am getting a a time format is in the "hh:mm" using time picker. eg(10:11) which is in string format and i am using Date() function which give me date is in something like
"Mon Aug 01 2016 01:32:03 GMT-0700 (Pacific Daylight Time)"
I want to replace "hh:mm"(01:32) from Date object with my string output of "hh:mm"(10:11) and then want to convert that into new Date object.
I also tried split and slice function but doesn't work.
If i get some guide about this will be very helpful.
I will be very helpful to all of you.
thanks
First of all, read up on Date.
In your case, the following code is a starting point.
var d = new Date();
d.setHours('10');
d.setMinutes('11');
Of course, you should exchange '10' and '11' with your picker data. Also, there are many other methods you can use on the Date object.
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 am trying to create a Date object in JavaScript, passing a string like this:
2014-11-30T00:00:00.0000000
However, the value of the Date object is:
Sat Nov 29 2014 17:00:00 GMT-0700 (Mountain Standard Time)
It changed it to 11/29 when I want 11/30. Is there any way I can make the date 2014-11-30, regardless of what time zone the browser is in?
Note: One possible workaround is to use the Date(year, month, day) constructor; however, I am constructing the data in a JSON string, which doesn't appear to support this.
EDIT:
Actually, I just did a test and created a date using Date(2015, 1, 1) and it gives me:
Mon Feb 02 2015 00:00:00 GMT-0700 (Mountain Standard Time)
So I can't even create a date that way and have it be the date I want. I don't understand why this is so difficult.
You can use Date.UTC
The UTC() method differs from the Date constructor in two ways.
Date.UTC() uses universal time instead of the local time.
Date.UTC() returns a time value as a number instead of creating a Date
object.
EDIT - why does SO insist on making links so hard to spot? That, up there, is a link to the docs in case that wasn't obvious.
EDIT 2 - I think I misunderstood. Try this:
var d = new Date('2014-11-30T00:00:00.0000000');
var utc = new Date(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds()
);
alert('d: ' + d + "\n" + 'utc: ' + utc);
I'm trying to get the current midnight date with Javascript and I use this:
var mydate = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 0, 0, 0, 0);
I receive this:
Sun Oct 26 2014 00:00:00 GMT+0100 (BST)
The problem is, that it's not British Summer Time anymore, but for some reason javascript still thinks it is. This is messing up my entire application, as I convert this into a timestamp like this:
mydate = Math.round(to_day.getTime() / 1000);
How can I change this?
JavaScript will always use your computer's locale to extract the date. It's using BST because this is the timezone that you have set (automatically perhaps?).
You can extract a UTC time string by calling the following function:
var d = new Date();
d.toUTCString(); // "Sun, 26 Oct 2014 11:46:11 GMT"
There are libraries that deal specifically with manipulation of date objects. One such tool I recommend you checkout is moment.js. It gives you the option to convert between timezones as well as a whole lot of other useful time/date related features.