Set last date before save angular js - javascript

I am trying to change date value before save. according to my requirement, i need to set last date of the month before save. this is my save function
$scope.saveCashBenefits = function (a) {
var olddate = $scope.newcashbenifit.toDate;
//for an example olddate = Wed Apr 01 2020 00:00:00 GMT+0530
}
In the above sample, the olddate is 'Wed Apr 01 2020 00:00:00 GMT+0530'
I need to modify this as 'Apr 30 2020'
if olddate is Month 'May', I need to set my month as 'May 31 2020'
basically i need to set last day of the month.
How I do it?

Related

Create date from number of days

I am trying to convert the number of days since Jan 01 1970 to JavaScript Date.
Here is the code snippet.
new Date(864e5 * parseInt(data[i].d));
//here data[i].d contains number of days.
I checked all the data by this.
console.log(typeof(data[i].d), data[i].d);
//prints
number 17674
but sometimes it unable to convert it into date.
Invalid Date {}
while for
number 17858
//outputs.
Fri Aug 17 2018 05:00:00 GMT+0500 (Pakistan Standard Time)
Thanks for your time.
You just have to add the number of days times the milliseconds in a day, like so:
var originalDay = new Date(864e5)
console.log(originalDay) //Thu Jan 01 1970 19:00:00 GMT-0500 (Eastern Standard Time)
var numOfDays = 7
var daysSince = new Date(864e5 + parseInt(numOfDays * 864e5))
console.log(daysSince) //Thu Jan 08 1970 19:00:00 GMT-0500 (Eastern Standard Time) --7 days later
To make this work for you, you would just have to replace that numOfDays with the values in your array.

Convert MMM YY string to date javascript

I have a value JUN 16 as string. I need to convert it to a date object. I need to find out last date of the month JUNE year 2016.
This is how I am converting a Date("MMM-YY") to Date("YYYY-MM-DD")
Input : Nov-17
run : new Date("Nov-17")
Output : Sat Nov 17 2001 00:00:00 GMT+0530 (India Standard Time)
now by adding a prefix to your date like below
input : 1-Nov-17 //add prefix to your string '1-'
run : new Date("1-Nov-2017")
output: Wed Nov 01 2017 00:00:00 GMT+0530 (India Standard Time)
Moment
run : moment("1-Nov-17").endOf('month').format("YYYY-MM-DD") //End day of the month
output: "2017-11-30"
I have a value "JUN 16" as string .I need to convert to date object
This is how you do it:
var date = new Date(2016, 5, 16);
console.log(date);
i need to find out last date of the month JUNE year 2016
And this is how you can do that:
// 5 is June
var date = new Date(2016, 5 + 1, 0);
console.log(date.getDay())
This tells you that the day is 4 which is "Thursday"

convert Millisecond to Date and Date to Millisecond

I want to convert date to millisecond as per follow I converted.
var d = new Date(1454911465467) \\ output : Mon Feb 08 2016 11:34:25 GMT+0530 (IST)
Now I want to convert using output to millisecond.
var d = new Date('Mon Feb 08 2016 11:34:25 GMT+0530 (IST)').getTime() \\output : 1454911465000
Expected output : 1454911465467
Is their any way to convert these type of millisecond?
Milliseconds are not specified in 'Mon Feb 08 2016 11:34:25 GMT+0530 (IST)'. The date precision here is down to seconds. Hence 467 milliseconds are missed in the second result.
You can check e.g.
var originalDate = new Date(1454911465467);
var clonnedDate = new Date(originalDate.getFullYear(), originalDate.getMonth(), originalDate.getDate(), originalDate.getHours(), originalDate.getMinutes(), originalDate.getSeconds(), originalDate.getMilliseconds());
document.write(clonnedDate.getTime());

SQL statement querying result which is within a time range

I want to do a sql statement which queries by timestamp using javascript.
Here is how i set my timestamp:
var startTime = new Date(year, month, day, 0, 0);
var endTime = new Date(year, month, day, 23, 59);
My sql statement is:
'SELECT * FROM proximate.user WHERE join_timestamp >= $1 ' +
'AND join_timestamp<=$2 ORDER BY user_id ASC';
$1 is startTime and $2 is endTime. Given if the startTime is Sat Dec 01 2012 00:00:00 GMT+0800 (SGT) and endTime is Sat Dec 01 2012 23:59:00 GMT+0800 (SGT), the executed statement returns results which include timestamp that is a day before the startTime.
Anyone has any idea why?
Thanks in advance.
My guess is that you receive proper results but forget to convert timestamp column of returned resultset to GMT+8 : "Sat Dec 01 2012 00:00:00 GMT+0800" = Fri Nov 30 2012 16:00:00 UTC". Another option - pass dates in UTC. Actually that depends on what you want to get: if you need all users who joined on December 1st GMT+8 you need to convert result set, if you need users who joined on December 1st UTC, pass UTC dates :"Sat Dec 01 2012 00:00:00 UTC"

Converting a date string into UTC+0530 format using javascript

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?
Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.
I tried this code and it returned proper date (In Indian Locale)
var d=Date.parse("14,Feb,2011");
document.write(new Date(d));
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
Here's an example of converting between different time zones.
<html>
<body>
<script type="text/javascript">
//Set you offset here like +5.5 for IST
var offsetIST = 5.5;
//Set you offset here like -8 for PST
var offsetPST = -8;
//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));
//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate = new Date(d.getTime() + (d.getTimezoneOffset()*60000));
//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST*60)*60000));
//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate= new Date(utcdate.getTime() - ((-offsetPST*60)*60000));
document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>
</body>
</html>
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time)
Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.
var d = new Date("14-Feb-2011");
this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

Categories

Resources