Check for date object with Angular Unit Tests - angularjs

If my expected result is a date object in the following format, how can I test for it?
Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Daylight Time)
I've tried
expect(theDate).toEqual( Date(Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Daylight Time) ) );
But I'm getting the following error on running the test :
Uncaught SyntaxError: missing ) after argument list
There are no other unclosed brackets casuing the problem. For example, if I change my tests to this:
expect(theDate).toEqual( 'Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Daylight Time)' );
It correctly fails with:
Expected Date(Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Standard Time)) to equal 'Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Daylight Time)'.

The JavaScript Date Object needs to receive a string in the constructor and you need a new in front of Date.
expect(theDate).toEqual( new Date("Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Daylight Time)") );
If you are unsure, you can always extract the compare value and output it for debugging.
var expectedDate = new Date("Thu Jan 22 2015 00:00:00 GMT+0000 (GMT Daylight Time)");
console.log('why am I doing it wrong?', theDate, expectedDate);
// expect(theDate).toEqual(expectedDate);

Related

how to return the right date using date picker react

when i select date 06/11/2022
Datepicker shows 06/11/2022
Console.log shows Sun Nov 06 2022 00:00:00 GMT+0100 (heure normale d’Europe centrale)
I want to get just Date remouving time by using
date.toISOString().split('T')[0] ==> 05/11/2022
what can i do to get the right date?
Thnaks

Converting Date in Snowflake

I have a Snowflake Stored Proc that uses Run_Date as a parameter. With the the code when I use the this date I get Tue May 03 2022 08:37:42 GMT-0500 (Central Daylight Time).
How can I convert this date to something like this - 2022-01-15 10:20:33.001 +0000

Convert date object to milliseconds in angularjs

How can I get the starting time of a particular dateobject in milliseconds? my dateobject is in below format.
Sun Oct 16 2016 21:33:35 GMT+0530 (India Standard Time)
My input
dateobject = Sun Oct 16 2016 21:33:35 GMT+0530 (India Standard Time);
Required Output
Sun Oct 16 2016 00:00:00 GMT+0530 (India Standard Time)
Irrespective of time, I want to get the starting time of this date in milliseconds(i.e Sun Oct 16 2016 00:00:00 GMT+0530 (India Standard Time)?
Sun Oct 16 2016 21:33:35 GMT+0530 (India Standard Time) -> Sun Oct 16 2016 00:00:00 GMT+0530 (India Standard Time)->1476556200000
I tried like this.
var date_form = $filter('dateobject')(toDate, 'yyyy/MM/dd');
(date_form=2016/10/16)
var full_date = new Date($filter('date')(date_form, 'fullDate'));
(full_date = Oct 16 2016 00:00:00 GMT+0530 (India Standard Time))
var milliseconds = full_date.getMilliseconds();
I am not getting the proper output? Is this correct one?
If I'm not wrong, you want to truncate time from date object and want milliseconds values of them.
For truncate time from date object, you don't need any angularJS code, you can do by javascript code only.
First approch
// Truncate time from exists object
toDate.setHours(0);
toDate.setMinutes(0);
toDate.setSeconds(0);
toDate.setMilliseconds(0);
console.log(toDate);
Second approach
// Create new object of date
full_date = new Date(toDate.getFullYear(), toDate.getMonth(), toDate.getDate());
getMilliseconds() method just giving part of milliseconds, its not total milliseconds. use getTime() instance of getMilliseconds() for get milliseconds.
full_date.getTime() // get milliseconds of start date

AngularJS change date format( JUN, Wednesday ) from Wed Jun 08 2016 05:30:00 GMT+0530 (India Standard Time)

I want to change date format from Wed Jun 08 2016 05:30:00 GMT+0530 (India Standard Time) to JUN, Wednesday.
I have tried, <p data-ng-bind = "'Wed Jun 08 2016 05:30:00 GMT+0530 (India Standard Time)'| date:'MMM' "></p>
I did not get the exact result(JUN, Wednesday), please help.
Your problem is you're trying to cast a string with date format. So, You need to cast first string to date, then move the hour for the timezone problem, finally apply the filter.
In your controller you must do something like:
# Step 0: Get a date from your string
var date = new Date('Wed Jun 08 2016 05:30:00 GMT+0530');
# Step 1: From the date move, for the Timezones problems.
$scope.date = new Date(date.valueOf() + date.getTimezoneOffset() * 60000);`
Then, in your view you can use {{ date | date:'MMM-EEEE' }}
You can check in this Plnkr: http://plnkr.co/edit/zselhzgL4njIWEXxTWh6?p=preview

Convert Date: Day Mon YYYY hh:mm AM CET

I have these dates, that I need converted to date:
Sat Nov 22 2014 01:01 AM CET
Mon Aug 18 2014 06:32 PM CEST
All the convert or cast functions I tried didn't work, maybe someone has an idea what to do?
In the end, I would need something like
YYYY-MM-DD HH:MM:SS or DD.MM.YYYY HH:MM:SS that doesn't really matter, but I would need them in the same timezone if at all possible...
Thank You for any ideas
SQL Server can convert that if you get rid of the day of the week at the beginning and the time zone at the end:
SELECT CONVERT(DATETIME, SUBSTRING('Sat Nov 22 2014 01:01 AM CET',4,LEN('Sat Nov 22 2014 01:01 AM CET')-7))
SELECT CONVERT(DATETIME, SUBSTRING('Mon Aug 18 2014 06:32 PM CEST',4,LEN('Mon Aug 18 2014 06:32 PM CEST')-7))
I'm not sure what you mean that you need them in the same time zone.

Resources