Javascript datetime format varying on different systems - javascript

I have a method to covert date to IST which is,
function GetCurrentIST() {
var dte = new Date();
dte.setTime(dte.getTime() + (dte.getTimezoneOffset() + parseInt(UtcOffset)) * 60 * 1000);
return dte.toLocaleString();
}
another function calls it like this,
function GetStartDate(Selected) {
var StartDt = new Date(GetCurrentIST());
StartDt.setDate(StartDt.getDate() + parseInt(Selected));
StartDt.setHours(0);
StartDt.setMinutes(0);
StartDt.setSeconds(0);
return StartDt;
}
The problem I'm facing is on my machine GetCurrentIST() returns date properly.
Eg. "10/12/2014, 12:26:37 PM"
and the StartDt I get as : Dec 10 2014 12:37:32 GMT+0530 (India Standard Time)
But on my friends machine GetCurrentIST() returns date as : "10/12/2014 12:28:40"
and StartDt as : Sun Oct 12 2014 12:29:09 GMT+0530 (India Standard Time)
Is this problem related to datetime format or something.
does anyone have any simple solution for this problem?

Related

Converting epoch time to GMT Date Time Javascript

I am currently using moment js in my application and not able to figure out how to convert epoch time to GMT date time. Providing my code below:
click: (event) => {
console.log(moment.utc(event.point.category).toDate());
}
event.point.category is providing epoch time which I want to convert to GMT date time object, but above code is converting it to local timezone date object.
For ex. 1606262400000 is getting converted to Tue Nov 24 2020 19:00:00 GMT-0500 (Eastern Standard Time) instead of GMT date time which is Wednesday, November 25, 2020 12:00:00 AM
You should get the (UTC) offset (in minutes) of the client to correct time
click: (event) => {
const offset = new Date().getTimezoneOffset();
const targetTime = moment.utc(event.point.category).toDate();
const offsetTime = new Date(targetTime.getTime() + offset * 60 * 1000);
console.log(offsetTime);
}
I believe all that you are missing is a toUTCString().
An epoch timestamp will be the same for Tue Nov 24 2020 19:00:00 GMT-0500 (Eastern Standard Time) and Wednesday, November 25, 2020 12:00:00 AM GMT
click: (event) => {
console.log(moment.utc(event.point.category).toDate().toUTCString());
}
To remain as a Date object, use moment.utc().format()
You can try something like this,
click: (event) => {
console.log(moment(moment.utc(event.point.category).toISOString()).utc());
}

JavaScript - Assigning Array Index to Return Value not working

I am trying to assign a return value to an element in an array, and for some reason it is overwriting both elements in the array to the same value instead of just the second element.
The datepicker I'm using for this can be found here, for reference.
http://www.eyecon.ro/datepicker/
function AddDays(date, days)
{
var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));
return newDate;
}
$('#nights').on('change', function(){
var $dp = $('#date');
var t = $dp.DatePickerGetDate();
console.log(t); // Output 1
t[1] = AddDays(t[0], $(this).val());
console.log(t); // Output 2
});
Output 1 : [Mon Jan 02 2017 00:00:00 GMT-0600 (Central Standard Time), Sun Jan 08 2017 23:59:59 GMT-0600 (Central Standard Time)]
Output 2 : [Mon Jan 09 2017 00:00:00 GMT-0600 (Central Standard Time), Mon Jan 09 2017 00:00:00 GMT-0600 (Central Standard Time)]
As you can see, when I try to assign the second element t[1] to equal the return of AddDays(t[0], $(this).val()) both dates get set the same exact date. However, the second element is supposed to be equal to the first element plus the number of days specified.
I also verified that the element $(this).val() is not zero. In my case that is in fact returning 7 when I run console.log($(this).val()); right before the assignment of t[1].
What am I doing wrong here?
Your function modifies the date that is passed in when you call setTime. You can remove the call to setTime to solve your problem.
function addDays(date, days) {
return new Date(date.getTime() + (days * 86400000));
}
Or make a brand new date object and call setDate or setTime on the copy.
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}

date string that will ignore timezone offset

I am at (UTS-05:00) Eastern Time (US & Canada)
i.e, new Date().getTimezoneOffset() == 300 seconds.
Now, I have a API endpoint (JSON) that returns a date string like this.
{
someDate: '2016-01-01T00:40:00.000+00:00'
}
Here, I pass it to Date constructor like this
var dateString = "2016-01-01T00:40:00.000+00:00";
var someDay = new Date(dateString);
console.log(someDay)
Mozilla Firefox console shows
Date {Fri Jan 01 2016 00:40:00 GMT-0500 (Eastern Summer Time)}
Google Chrome console shows
Thu Dec 31 2015 19:40:00 GMT-0500 (Eastern Standard Time)
Chrome is taking the TimezoneOffset into consideration and Firefox is not. What can I do to get a Date that doesn't take Offset into consideration like FireFox in Chrome?
You can do it by:
var dates = '2016-01-01T00:40:00.000+00:00'.split(/-|T|:/);
var newDate = new Date(dates[0], dates[1]-1, dates[2], dates[3], dates[4]);
This hack works (not very clean, but does the job)
var dateString = '2016-07-27T01:40:30';
var dateParts = dateString.split(/-|T|:/);
var saneDate = new Date(
+dateParts[0],
dateParts[1] - 1,
+dateParts[2],
+dateParts[3],
+dateParts[4],
+dateParts[5]);
console.log(saneDate);

JavaScript: Comparing two dates no output

I would like to compare the given date in the below format in JaveScript. I have tried the following,
Thu May 19 2016 00:00:00 GMT+0530 (India Standard Time)
Thu May 20 2016 00:00:00 GMT+0530 (India Standard Time)
var ExpiryDate = userAccount.ExpiryDate();
var datetoday = new Date();
var Expired = (DateTime.Compare(ExpiryDate, datetoday) == -1 ) ? true : false;
//if expiry date is less than today date then var expired should be true
But didn't worked. I could not compare those two dates. It results in un handled exception. Is there any other way to do this date comparison in JaveScript ?
I have referred the following answers in SO but they are in different date format. So that I have raised this question,
javascript compare two dates and throw an alert
Javascript comparing two dates has wrong result
Compare two dates in JavaScript
Javascript compare two dates to get a difference
Any suggestion would be helpful.
var date = new Date();
//# => Fri May 20 2016 16:09:43 GMT+0530 (India Standard Time)
var date2 = new Date();
date2.setDate(date.getDate() - 1);
//# => Thu May 19 2016 16:09:43 GMT+0530 (India Standard Time)
date > date2 //# => true
use getTime()
var date1 = (new Date("20 May 2016")).getTime();
var date2 = (new Date("19 May 2016")).getTime();
date1>date2
You will find some good method here

.Net DateTime() to Date using javascript

I am getting the Date value as DateTime from code behind to my javascript. I have to handle it in javascript only, below is the string I am currently getting from code behind to my Client side.
"Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)"
How to change this string to Date only from javascript. I want the format as "dd/mm/yyyy".
Try to use moment.js
var day = moment("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)", "dd/mm/yyyy");
Ok.
Just got the solution.
below is the javascript code taking argument as the .Net DateTime() string
function formatTheDate(strDate) {
if (strDate != '') {
var date = new Date(Date.parse(strDate));
var formatedDate = (date.getUTCDate()) + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
return formatedDate;
}
}
fiddler : http://jsfiddle.net/xL5rdce9/
If you cannot change it server side, then start here:
alert(new Date(Date.parse("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)")))
then according to your own answer, you can end up here:
function pad(num) {
return ("0"+num).slice(-2);
}
var myCSharpString = "Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)";
var date = new Date(Date.parse(myCSharpString));
alert(pad(date.getUTCDate()) + "/" + pad(date.getMonth() + 1) + "/" + date.getFullYear());
You can simply use MomentJS for dates.
var format = moment("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)").format("DD-MM-YYYY");
alert(new Date(format));
<script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment.min.js"></script>

Categories

Resources