I am using date function in javascript to get the currentDate.
as
var currentTime =new Date()
But it is giving me different response in IE and Firefox as
Consider for today's Date
IE - Tue 24 Apr 17:05:22 UTC 0530 2012
Mozilla - Tue 24 Apr 17:05:22 2012 GMT 0530
The Problem is that I Want to convert today's date which is in "String" to "Date" into format - EEE mmm-dd hh:mm:ss IST yyyy
But as i m getting different response in this browser I m not getting how to apply simpleDateformatter to convert current date and time to the suitable format .
You can use functions of object Date. For example:
var currentTime = new Date();
var date = currentTime.getDate();
var day = currentTime.getDay();
var hours = currentTime.getHours();
With help this functions you can convert current date and time to the suitable format.
Related
I need to send a date value to the server in ISO Format "YYYY-MM-DDTHH:mm:ss.SSS[Z]" I don't need the time details so I am setting them as zero.
For that I am using the below code
var today = new Date();
var todayWithTimeAsZero = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
I get todayWithTimeAsZero as Tue Jul 25 2017 22:06:03 GMT+0530 (India Standard Time)
Now how do I convert this date into an ISO format. I researched everywhere but no luck.
I tried var day = todayWithTimeAsZero.toISOString(); but this creates a new date object with the time values populated like so 2017-07-24T18:30:00.000Z. Also I have momentjs in my project may be in some way I can use that.
With moment.js you can get the current date as UTC, then set the time values to zero and get the ISO string:
moment() // current date
.utc() // convert to UTC
.hours(0).minutes(0).seconds(0).milliseconds(0) // set time values to zero
.toISOString() // format to ISO8601
The value of the formatted string is 2017-07-25T00:00:00.000Z.
You can also use the setUTCxxx methods of Date:
var today = new Date();
today.setUTCHours(0);
today.setUTCMinutes(0);
today.setUTCSeconds(0);
today.setUTCMilliseconds(0);
today.toISOString() will be 2017-07-25T00:00:00.000Z.
If you create a Date then zero the UTC time and get just the date, it will be a different date from the local date for the period of the timezone offset. For someone in UTC+1000 the UTC date is yesterday until 10:00. For users who are UTC-0600 it will be "tomorrow" after 18:00 (6 pm).
Anyway, without any library you can do:
var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());
Here is date that I received from Backend "2017-05-23T18:30:00",
This works fine in Chrome:
Call: new Date('2017-05-23T18:30:00').toString()
Result: "Wed May 24 2017 00:00:00 GMT+0530 (India Standard Time)"
But on the Internet Explorer:
Call: new Date('2017-05-23T18:30:00').toString()
Result: "Tue May 23 2017 18:30:00 GMT+0530 (India Standard Time)",
What to do get local date time from UTC date in Internet Explorer as I am getting in Chrome?
You can use moment.utc to parse your input cross browser. Then you can use format() to display your moment object. If you need to convert moment object to JavaScript date you can use toDate() method.
Use local() if you want to convert your moment to local time.
See Local vs UTC vs Offset for additional information.
Here a live sample:
var input = '2017-05-23T18:30:00';
var m = moment.utc(input);
console.log(m.format());
console.log(m.toDate().toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
IE browser will take in the format of 'mm-dd-yy'. if you give in the format of 'yy-mm-dd' it results Invalid Date.
use following function to convert UTC to LocalDate.
function convertUTCDateToLocalDate(utcDate) {
var formattedDate = utcDate.getMonth()+'-'+utcDate.getDate()+'-'+utcDate.getFullYear();
var hours = utcDate.getHours();
var minutes = utcDate.getMinutes();
var seconds = utcDate.getSeconds()
var newDate = new Date(formattedDate + ' ' + hours + ':' + minutes+":"+seconds+" UTC");
return newDate;
}
var localDate = convertUTCDateToLocalDate(yourUTCDate);
I need to get format like below
19 Aug 2015 04:22:36 GMT
I have following code
var dt = '2015-08-19 04:22:36';
alert(new Date(dt).toUTCString().substr(4))
It returns me
18 Aug 2015 22:52:36 GMT
where as it should return
19 Aug 2015 22:52:36 GMT
What is wrong in my code
JsFiddle
As per answers below it seems it's converting the date to UTC date time.
I have date in UTC format in database. So please if some one could suggest the desired format without using toUTCString()
Update
Tried following
var dt = '2015-08-19 04:22:36 UTC';
alert(new Date(dt).toUTCString().substr(4))
It gives me liddate in FireFox and IE, chrome it is fine
Solved changed date string to
var dt = '2015/08/19 04:22:36 UTC';
Thanks
The toUTCString() converts your date to UTC so the outcome is correct!
Change your string to this var dt = '2015-08-19 04:22:36 UTC'; (notice the UTC)
or use the Date.UTC() function:
alert(new Date(Date.UTC(2015, 09, 19, 04, 22, 36)).toUTCString().substr(4))
Notice that the month is 0 based (0 -11) so to get August
you need to increase your monty by 1
From your profile, I can see you are 5 hours and 30 minutes ahead of UTC, so the var dt = '2015-08-19 04:22:36'; returns a Date object which is 5 hours and 30 minutes ahead of UTC time i.e. your local time. When you convert this date to UTC, it subtracts 5.5 hours and returns the Date object which is 22:56 previous night. The time part 04:22:36 is actually 4:22 a.m. or 4:22 a.m. at morning.
I'm using the Angular UI Bootstrap Datepicker and Timepicker (https://angular-ui.github.io/bootstrap/), and I want to combine both of those values and get the current millis. I get the date in this format: "2015-05-19" [YYYY-MM-DD"] and the time as this: "10:57:19" [HH:MM:SS].
I saw that there is a Date.parse() method where you can get current millis but I couldn't find a way where you can include a time as well, and was wondering if there was a method for this?
Since you already have a date and a time, you could use these to construct a date string:
var date = '1970-01-01';
var time = '00:00:00';
var dateString = date + 'T' + time; // '1970-01-01T00:00:00'
var parsedDate = Date.parse(dateString); // 0 Milliseconds
The 'T' is for handling timezones. Since I'm located in central Europe, my timezone offset is GMT+1 (Central European Time).
var newDate = new Date(parsedDate); // Thu Jan 01 1970 01:00:00 GMT+0100 (CET)
looks like the answer would be: Date.parse('MM DD YYYY HH:MM:SS');
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
but you probably need to be wary of the time zone.
Im getting time response value as from php
00:00:00 or 10:10:10
only time values(24 hours format)
I need to convert this as javascript date object . then only i can able to use with javascript . How can achive this?
Using Moment js we can achieve
var dateObj= moment('15:00:00', 'HH:MM:SS').toDate();
console.log(dateObj);
Out put
Mon Dec 01 2014 15:00:00 GMT+0530 (India Standard Time)
A JavaScript Date object requires a Date as well as a time, so you will need to provide that with your received PHP time.
I'm guessing that you'd just want to use the current Day, Month and Year, so we will:
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
var msecs = d.getMilliseconds();
Of course, you can also use the UTC methods instead.
Now that you have all the information necessary for a Date object, but the hours, minutes and seconds, we can create those from your PHP-provided string:
var hours = /\d\d/.exec(str);
var mins = /(?!^)\d\d/.exec(str);
var secs = /\d\d$/.exec(str);
Now we have everything necessary to create our Date object, we can do so:
var ourDate = new Date(year, month, day, hours, mins, secs, msecs);
Ta - da!
See a working fiddle.