how to convert milliseconds to gmt time using javascript - javascript

I have a project where if the end date is set for February 12 (can be set to any future date), the following is obtained from the API response.
project: {endDateTime:"1518393600000"}
For UTC time and date, this response corresponds to Mon Feb 12 2018 00:00:00
For local time and date, the response corresponds to Sun Feb 11 2018 19:00:00 (GMT - 05:00)
On the UI, I need to show the end date as Feb 12, 2018, but the date is getting converted to the local date and time zone and shows Feb 11 as the end date. My code is below:
var d = new Date();
var c = d.setTime(parseInt($scope.project.endDateTime));
$scope.endDateTime = c;
In the html
<div> {{endDateTime}} </div>
I tried modifying the code in the following way but it did not work.
var d = new Date($scope.project.endDateTime);
var c = d.getUTCDate();
$scope.endDateTime = c;
I tried to tune the code in other ways but could not get it to work. I know similar questions have been asked before but still could not get it to work, even after spending several hours. Maybe I am missing something very trivial. Some help would be greatly appreciated. :)

I have figured out the solution. My code and explanation is as follows
var d = new Date(parseInt($scope.project.endDateTime));
var c = c.toUTCString();
var endDate= c.split(" ");
$scope.endDateTime = endDate[2] + ' ' + endDate[1] + ',' + ' ' + endDate[3];
And the html is
<div> {{endDateTime}}</div>
I needed to parse the string that came back from the API response. Missed this part and also the correct method is toUTCString(), not getUTCDate().
So basically what happens is if the end date is set to Feb 12(in this case, it is dynamic though), the API returns
project: {endDateTime:"1518393600000"}
The first line of the code parses the string and if I do a console.log(d) I get this: Sun Feb 11 2018 19:00:00 GMT-0500 (Eastern Standard Time).
Now I need to display the time in GMT so the toUTCString() method was used in the second line and doing console.log(c) I get: Mon, 12 Feb 2018 00:00:00 GMT
The requirement is to show the date as Feb 12, 2018 on the UI, so I split the string in the third line and if i do console.log(endDate) I get
["Mon,", "12", "Feb", "2018", "00:00:00", "GMT"]
From the output of the third line, it was pretty easy to show the required date format. Hope this helps.

Related

unable to get the date format

I'm trying to change the date format in javascript where in a variable i m getting the date value like this "11/09/2019" and want to change in a format like this "11-09-2019".but somehow i'm getting this format "sat nov 09 2019" whereas it should be "tue sep 11 2019".
can anyone help me. any help would be appreciated.
var alt_date1 = "11/09/2019";
var date = new Date(alt_date1);
alert(date);
output: Sat Nov 09 2019 00:00:00 GMT+0530 (India Standard Time)
If I understand your question right, you want a different format i.e., "-" instead of "/" and you're also getting the wrong date? 2 days ahead? Depending on your environment (browser, node, etc) the Date API can behave differently. I think this link might help you: JavaScript's getDate returns wrong date (Not sure though, some answers pertain to the time zones it uses)
As for formatting look here:
https://codehandbook.org/javascript-date-format/
Example from that site:
let current_datetime = new Date()
let formatted_date = current_datetime.getDate() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getFullYear()
console.log(formatted_date)
// output
"19-10-2018"
It's a little bit tricky to understand the question fully, but if you don't need a Date object, and you want to just format the string as 11-09-2019 it can be achieved with the simple replace function like this:
alt_date1.replace(/\//g, '-')

How to parse a date with momentjs?

I'm trying to parse a date in momentjs, in particular this is my goal:
Ven Nov 13 2015 09:00:00
Now I'm using FullCalendar and when I get the .start date it's returned this:
Fri Nov 13 2015 00:00:00
how you can see in my bottom code, I'm format the calendarDateStartTemp to utc for remove the GMT. In the next step, I transform the object in italian timezone, but this seems not working. Anyway, I've in workingPlan[selDayName].start the hour to edit, in particular this is the value: 09:00:00, see the code:
var calendarDateStartTemp = $calendar.fullCalendar('getView').start;
var calendarDateStart = moment(calendarDateStartTemp).utc().format("ddd MMM DD YYYY HH:mm:ss");
var calendarDateEnd = moment.lang('it');
calendarDateEnd = moment(moment(calendarDateStart).format("YYYY-MM-DD") + ' '
+ workingPlan[selDayName].start).format('ddd, D MMM YYYY HH:mm:ss');
now the problem's that I get this result:
Fri, 13 Nov 2015 09:00:00
instead of this:
Ven Nov 13 2015 09:00:00
how you can see the date returned is in english language, but I don't know why moment.lang now working. I say that it's deprecated so I've also tried with moment.locale but I've the same problem. How I can fix this?
NB: the language is italian
var data = moment().locale('it').format('llll');
alert(data);
By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js.
I'm assuming you have both moment.js and monement+locales.js included, the scripts are found here. http://momentjs.com/

Javascript Date issue, incorrect timezone

I got this strange JavaScript bug that I can seem to work arround or fix.
I am using some code to make 2 JavaScript dates, to insert events into a calendar component.
The dates are built the following way:
var endDate = new Date();
var startDate = new Date();
startDate.setDate(startDateDay);
startDate.setMonth(startDateMonth);
startDate.setFullYear(startDateYear);
startDate.setHours(2, 0, 0, 0);
endDate.setDate(endDateDay);
endDate.setMonth(endDateMonth);
endDate.setFullYear(endDateYear);
endDate.setHours(2, 0, 0, 0);
So, the dates are built using integers. These integers are determined by input, and using the debugger I can see 100% positive they are coming in correctly.
Now, ill describe 3 walkthroughs, 2 where it goes correctly and 1 where it goes wrong.
Using the following input:
endDateDay = 20
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Tue Oct 20 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)
Using this input:
endDateDay = 13
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Tue Oct 13 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)
Now, using this input:
endDateDay = 27
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Mon Oct 27 2014 02:00:00 **GMT+0100** (W. Europe Standard Time)
As you can see, for some strange reason the TimeZone is off. This gives errors in my application, and I need to find a way to get it fixed. Though, I cannot find any solution to it, let alone understand why it is actually happening.
PS: I am using Google Chrome
The answer was indeed the difference in the daylight savings time, which I completly oversaw. Thanks to finding this out I also found a solution to my problem.
I used this link to further assist me, might it help someone in the future:
http://javascript.about.com/library/bldst.htm
Cheers!

Convert GMT time to local time

Am getting a GMT time from my server in this format
Fri, 18 Oct 2013 11:38:23 GMT
My requirement is to convert this time to local time using Javascript, eg:/ if the user is from India, first i need to take the time zone +5.30 and add that to my servertime and convert the time string to the following format
2013-10-18 16:37:06
I tried with following code but not working
var date = new Date('Fri, 18 Oct 2013 11:38:23 GMT');
date.toString();
Please help me to solve this issue, Thanks in advance
This worked for me:
<script>
var strDateTime = "Fri, 18 Oct 2013 11:38:23 GMT";
var myDate = new Date(strDateTime);
alert(myDate.toLocaleString());
</script>
Please take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp for all further date time manipulations, from the date object myDate.

Date() method of javascript while passing a timestamp adds a day to the result

I am trying to get a date out of the time stamp I have (which is like the format : 2012-08-31T18:30:00, coming from a date picker)
var value = "2012-08-31T18:30:00";
var date = new Date(value);
The result of this(date) is: Sat Sep 01 2012 00:00:00 GMT+0530 (India Standard Time)
Here Date() function is adding a day to the given date. I have tried this for different inputs but always I get the result with one day added to it. I tried googling and searched Stack overflow too, but couldn't get a valid answer. Can anyone tell me why it is happening and how to resolve this?
Thanks,
Riswan
It's your local timezone that's throwing off the value. You can find more out about UTC here.
The value returned by toUTCString is a readable string in American English in the UTC time zone. The format of the return value may vary
according to the platform. The most common return value is a RFC-1123
formatted date stamp, which is a slightly updated version of RFC-822
date stamps.
var value = "2012-08-31T18:30:00";
var date = new Date(value);
date = date.toUTCString()
EXAMPLE
EDIT:
What I can gather from the comments you posted, you may be looking for something more like THIS EXAMPLE:
var value = "2012-08-31T18:30:00"; //string value
var dateTime = value.split("T"); //split on T
var date = dateTime[0];
date = new Date(date); //set the date
var time = dateTime[1];
time = time.split(":"); //get the time into an array
//use set hours to set the time to 18:30:00:00 (hh:mm:ss:mm)
date.setHours(time[0], time[1], time[2], 0)
console.log(date);
"2012-08-31T18:30:00" is referred to Greenwich Mean Time (GMT) / UTC.
"2012-08-31T18:30:00" + India (UTC+5:30) = Sat Sep 01 2012 00:00:00 GMT+0530 (India Standard Time)
Because the value of the timestamp is in GMT, and Date() returns the local timestamp.
Use date.toGMTString() to keep it in GMT format.
Sat Sep 01 2012 00:00:00 GMT+0530 is in GMT + 5h30
And it's actually 2012-08-31T18:30:00 plus 5h30

Categories

Resources