How to dynamically get the start and end date of the week - angularjs

I'm using ionic framework.Is there a way to dynamically get the start and end date of the week when user selects a random date.

Based on this answer I've made a configuration based on your needs. The code is in vanilla js but you can easily translate it to angular code, since functionality is the same.
The function to find the first and last day of the week, based on user selected date:
function getFirstLastDayOfWeek(userDate) {
let result = {};
let curr = new Date(userDate); // get current date
let first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
let last = first + 6; // last day is the first day + 6
result = {
firstDay:new Date(curr.setDate(first)).toUTCString(),
lastDay:new Date(curr.setDate(last)).toUTCString()
};
return result;
};
And here's a working fiddle.

var days=new Date().getWeek();
console.log(days[0].toLocaleDateString() + ' to '+ days[1].toLocaleDateString())
Dynamic date
var days=new Date("10/01/2017").getWeek();
console.log(days[0].toLocaleDateString() + ' to '+ days[1].toLocaleDateString())

Please mention your ionic version.
Using moment.js would solve your problem easily.
Lets say you have a random date with you and wants to find start of week.
moment(<randomDate>).startOf('isoWeek');
End of the week
moment(<randomDate>).endOf('isoWeek');

Related

React - read dates from excel file

I am having a weird issue when I was reading dates from excel files in a react application. When I read it, it comes out as some form of float but what's weird is that it only does this for 1th to 12th date of the month from 13th till 31th is fine.
What I mean is that a date like 01-01-81 gets converted to 29587.00013888889
but 13-01-81 remains in its original.
I found this one solution from here How to read date from an excel file in JavaScript. But it does not give back the original value.
Really appreciate any kind of help. Cheers.
The problem was that excel has its own way for dates and we need to convert it, into the format we need.
This is the method I used
const ExcelDateToJSDate = (date) => {
let converted_date = new Date(Math.round((date - 25569) * 864e5));
converted_date = String(converted_date).slice(4, 15)
date = converted_date.split(" ")
let day = date[1];
let month = date[0];
month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(month) / 3 + 1
if (month.toString().length <= 1)
month = '0' + month
let year = date[2];
return String(day + '-' + month + '-' + year.slice(2, 4))
}
You can simply pass the excel date and change the function according to the date format you need.

Set a date to a month not from the current date using moment

I need to set the date to month “01 April” of that current year so that at a later stage I can calculate the number of days from the current date. Is this the ideal way to do it?
let year = moment().year();
let newDt = moment(year).add(3, 'months').format('MM-DD-YYYY') // Just set the date to 1st April
It is very simple with moment. Just do moment().date(1).month(3)
Edit : I have added a full example
const aprilFirst = moment().date(1).month(3);
alert('Date difference is ' + moment().diff(aprilFirst, 'days') + ' days');
<script src="https://momentjs.com/downloads/moment.min.js"></script>

Date comparison validation in angular

I have 2 dates: 1.start and 2.end
format is like this.
12/4/2017 console.log(startDate);
12/20/2017 console.log(endDate);
I am writing a validation to check if the end date is bigger than start date throw error,but it is not working.
this is what i have tried:
var startDate = new Date(this.formB['startDateVal']).toLocaleDateString();
var endDate=new Date(this.formB['dueDateVal']).toLocaleDateString();
this is my condition:
if(endDate<startDate){
this.bucketMsgClass='fielderror';
this.bucketSuccessMsg = 'End Date is must lower than Start Date.';
}
where am i doing wrong.?
I've always just subtracted one of the dates from the other. If the result is negative, then Date 1 is before Date 2.
var d1 = new Date("12/12/2017");
var d2 = new Date("12/13/2017");
console.log(d1 - d2) // -86400000 (exactly 1 day in milliseconds)
So
if (d1 - d2 < 0) {
// d1 is smaller
}
Going through this link that explains comparing dates in javascript would probably help you understand the problem and solve it.
Compare two dates with JavaScript

Converting day, weeknr and year to date

I've been given an excel document in which worktime information is noted, this document contains certain columns which are being read by using SSIS in visual studio, after that the information is writen to a Database.
The week and year column contain the week number and the year, the columns Monday up to Friday contain information about how many working hours have been spent on a certain task on that day of the week.
What I'd like to do is take the WeekNr, Year and Day and convert these into a date. I've been trying to accomplish this by using a script component that converts a day number, week number and year to a date but so far I haven't been able to get the day number from the columns. In my opinion it would work best if used with a start and end date taking the first and last date of that week.
So my question is if someone knows how to accomplish this, or if I should try a different approach.
The script component:
public override void Input0_ProcessInputRow(Input0Buffer Row, CultureInfo cultureInfo, int day )
{
DateTime firstDayOfYear = new DateTime(Int32.Parse(Row.Jaar), 1, 1);
int firstWeek = cultureInfo.Calendar.GetWeekOfYear(firstDayOfYear, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
int dayOffSet = day - (int)cultureInfo.DateTimeFormat.FirstDayOfWeek + 1;
Row.TaskDates = firstDayOfYear.AddDays((Int32.Parse(Row.Week) - (firstWeek + 1)) * 7 + dayOffSet + 1);
}
Based on this answer, I think you want something like the following. This result gives you Monday's date, so you can just AddDays based on the column day of the week.
DateTime jan1 = new DateTime(Int32.Parse(Row.Jaar), 1, 1);
int daysOffset = DayOfWeek.Monday - jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstMonday, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
var weekNum = Int32.Parse(Row.Week);
if (firstWeek <= 1)
{
weekNum -= 1;
}
var mondaysDate = firstMonday.AddDays(weekNum * 7);
var tuesdaysDate = mondaysDate.AddDays(1);

Total number of days in angular?

I'm using angular datefilter to output a countdown. I do it like this:
<p class="clock">{{timeleft| date:'dd'}}<span>:</span>{{timeleft | date:'HH'}}<span>:</span>{{timeleft | date:'mm'}}<span>:</span>{{timeleft | date:'ss'}}</p>
$scope.timeleft contains a value that is calculated from taking launch-date minus current date.
Currently, there is more than one month left before the countdown reaches 0. I would like to show the number of days in total, that is, more than the number of days in the current month.
This is more a question about calculating date differences, than something to do with angular. I have created a simple fiddle for you here: http://jsfiddle.net/IgorMinar/ADukg/
Basically you can calculate a date difference between two dates like this:
var dstring = '2014-03-09'; // date to check against the current date
var oneDay = 24*60*60*1000;
var diff = Math.floor(( Date.parse(dstring) - new Date() ) / oneDay);
Then just plug the value into angular any way you like.

Resources