new Date always returning 1 day behind - angularjs

I have the following code for new Date() along with a function that formats the new Date() to something a bit more digestible:
data.onbCase.push({
first_name: onbCase.getDisplayValue('subject_person.first_name'),
last_name: onbCase.getDisplayValue('subject_person.last_name'),
start_date: formatDate(new Date(onbCase.getDisplayValue('hr_profile.employment_start_date')))
});
function formatDate(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var day = date.getDate(),
monthIndex = date.getMonth(),
year = date.getFullYear();
return new Date(monthNames[monthIndex].substr(0,3) + ' ' + day + ', ' + year);
}
<td ng-show="c.options.start_date" title="{{item.start_date}}">{{item.start_date | date:'mediumDate'}}</td>
However, this always returns a date that is one day behind the correct date. Can someone explain why this happens and how to fix it? I know I can simply add 1 to the getDate():
var day = date.getDate() +1;
This seems incorrect though...any advice?

For more information see this question: Is the Javascript date object always one day off?
The new Date() string parser is not reliable, because you get different results for different string date formats.
new Date()
// Fri Feb 15 2019 15:08:14 GMT-0600 (Central Standard Time)
new Date("2019-02-15")
// Thu Feb 14 2019 18:00:00 GMT-0600 (Central Standard Time)
new Date("2019-02-15 00:00:00")
// Thu Feb 15 2019 18:00:00 GMT-0600 (Central Standard Time)
new Date("02/15/2019")
// Fri Feb 15 2019 00:00:00 GMT-0600 (Central Standard Time)
Some date formats, especially if you don't include time information, will treat your date as originating in Greenwich Mean Time and then offset your date by your computer's locale. This will result in the date being offset by negative 6 hours(CST) effectively altering your day to the day before.

Related

get Dates In Range in rescript and Daylight Saving Time

i have a calendar in my site which take a start date and end date and pass them into a function who calculates the dates between .
lets sat we have the start date Mon Mar 29 2021 03:00:00 GMT+0300 (Eastern European Summer Time) and the end date is Mon Apr 05 2021 03:00:00 GMT+0300 (Eastern European Summer Time) ; this function should return ["30/3/2021","31/3/2021","1/4/2020","2/4/2020","3/4/2020","4/4/2020"]
let getDatesInRange = (start, end) => {
let dates = ref([])
let current = ref(start)
while current.contents <= end {
dates := dates.contents->Js.Array2.concat([current.contents->toUTCDateString])
current := {
let date = current.contents->toUTCDateString->Js.Date.fromString
date->Js.Date.setDate(date->Js.Date.getDate +. 1.0)->ignore
date
}
}
dates.contents
}
and this is toUTCDateString function which take a date and give the string version of it
let toUTCDateString = date => {
let date = date->External.unSafeCastToJsObject
date["toISOString"]()["split"]("T")[0]
}
These functions where working fine until The time has changed for Daylight Saving Time; we gain an hour so the day stuck there in for some reason
Any body face this issue before and who to deal with such time issues ?

how to convert date object in 14 Dec 2020 from database in reactjs

When I tried to convert 2020-12-14 to 14 Dec 2020 by using
1st Method
<small>{item.date}</small>
2nd Method
{new Date(item.date).toLocaleString()}
then I got below output
2020-12-14
12/14/2020, 5:30:00 AM
Is there any way to convert the date format from 2020-12-14 to 14 Dec 2020.? in reactjs
A small modification to this elegant answer by Dave splits the toString date string into an array and formats it into the result you want. Check the code below:
const date = new Date(2020, 11, 14).toString().split(" ");
// ["Mon", "Dec", "14", "2020", "14:05:53", "GMT+0100", "(Central", "European", "Standard", "Time)"]
console.log(date[2] + " " + date[1] + " " + date[3]);
// 14 Dec 2020
using moment package
moment(moment('2020-11-18', 'YYYY-MM-DD')).format('DD MMM YYYY');

Parsing this format of data into a new one?

I have a format which returns me some dates and I need to parse it into something else which I find a little bit complicated.
The data format is Mon Dec 24 2018 9:00:00 as a startDate for example and Friday Dec 28 2018 17:00:00 as an endTime for example. What happens here is that I select I want someone to start on Monday at 9 until 17 everyday, but what my data does is makes it look like he's working non-stop.
I have tried mapping over it and putting it onto objects with days of the week and start and end times, but I ran into a problem because I create the object like
dates : {
monday: {
start: 9:00,
end: 18:00
},
tuesday: {
start:9:00,
end:18:00
}
// etc for everyday of the week
}
But, what if I only need Monday through Thursday, for example, that would be a problem. Anyone has any idea, how could I do that, in any other way? I was thinking about using moment.js.
I'm not sure if I understand your question, but I think you want to list all days between two different dates, here is an example of function that iterates over days by using moment.isSameOrBefore and moment.add functions, hope this help:
function toDays(startDateString, endDateString) {
const startDate = moment(startDateString, 'dddd MMM DD YYYY');
const endDate = moment(endDateString, 'dddd MMM DD YYYY');
const dates = {};
while(startDate.isSameOrBefore(endDate, 'day')) {
const currentDay = startDate.format('dddd');
dates[currentDay] = {start:'9:00', end:'18:00'};
startDate.add(1, 'days');
}
return dates;
}
const result = toDays('Monday Dec 24 2018', 'Friday Dec 28 2018');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

Jasmine date expect always returning false

I have a test written in Jasmine test runner:
it("Expect 'due date' to be 14 days from today", function () {
var dateNow = new Date();
scope.dateOfService(dateNow);
expect(scope.DueDate == new Date(dateNow.setDate(dateNow.getDate() + 14))).toBeTruthy();
});
However this always returns false? Is there something I'm doing incorrect?
To 'debug' this I also ran:
expect(scope.DueDate).toBe(new Date(dateNow.setDate(dateNow.getDate() + 14)));
And this returns:
Expected Date(Tue Nov 11 2014 08:20:23 GMT+0000 (GMT Standard Time)) to be Date(Tue Nov 11 2014 08:20:23 GMT+0000 (GMT Standard Time))
var dateNow = new Date();
scope.dateOfService(dateNow);
var dateReturned = new Date(scope.DueDate);
var dateAdd = new Date(dateNow.setDate(dateNow.getDate() + 14));
expect(dateReturned).toEqual(dateAdd);
The main point was to ensure it was .toEqual rather than .toBe.

Date format issue in Firefox browser

My code
for(n in data.values){
data.values[n].snapshot = new Date(data.values[n].snapshot);
data.values[n].value = parseInt(data.values[n].value);
console.log(data.values[n].snapshot);
}
here console.log shows perfect date in Chrome as 'Thu Aug 07 2014 14:29:00 GMT+0530 (India Standard Time)', but in Firefox it is showing as 'Invalid Date'.
If I console.log(data.values[n].snapshot) before the new Date line, it is showing date as
2014-08-07 14:29
How can I convert the date format to Firefox understandable way.
The Date object only officially accepts two formats:
Mon, 25 Dec 1995 13:30:00 GMT
2011-10-10T14:48:00
This means that your date 2014-08-07 14:29 is invalid.
Your date can be easily made compatible with the second date format though (assuming that date is yyyy-mm-dd hh:mm):
for(n in data.values){
n = n.replace(/\s/g, "T");
data.values[n].snapshot = new Date(data.values[n].snapshot);
data.values[n].value = parseInt(data.values[n].value);
console.log(data.values[n].snapshot);
}

Resources