I want to calculate date difference between two dates with angular js. I used this code:
$scope.formatString = function(format) {
var year = parseInt(format.substring(0,5));
var month = parseInt(format.substring(6,8));
var day = parseInt(format.substring(9,10));
var date = new Date(year, month-1, day);
return date;
}
$scope.dayDiff = function(fromdate,todate){
var date2 = new Date($scope.formatString(fromdate));
var date1 = new Date($scope.formatString(todate));
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}
But it is right for within same month but it can't do right calculation between two dates in different months.
For example date1 = 2016.2.12, date2 = 2016.2.18 (Ok)
For example date1 = 2016.2.12, date2 = 2016.3.1 (It is not right)
Why not try using the String split function (docs) to parse your date, split will produce an array of the year, month, and day in that order based on the format you're inputting. From there you can reference the array pieces by index, a bit easier than trying to get your substring offsets lined up correctly:
$scope.formatString = function(format) {
var pieces = format.split('.'),
year = parseInt(pieces[0]),
month = parseInt(pieces[1]),
day = parseInt(pieces[2]),
date = new Date(year, month - 1, day);
return date;
}
Take a look at this working example: http://plnkr.co/edit/GgXjqA76IX5bzQEP56ux?p=preview
It is better to use a ready-made library. Because you do not know that in the future you will still need to work with dates. I recommend sugarjs.
The difference between the dates can be found in the following manner.
Date.range('2012.01.01', '2013.01.01').every('days').length
Related
I have 2005-12-03T18:30:00.000Z this format of date in mongodb.I am using react for frontend .
The problem is i want to display date in dd/mm/yy format .How i can achieve this?
Let's say you get the date from MongoDB and then save in the state, then next in your code you can use momentjs date library, then simply format the date to dd/mm/yy like:
const dateFromDB = '2005-12-03T18:30:00.000Z'
const formattedDate = moment(dateFromDB).utc().format('DD/MM/YY')
console.log( 'Date From DB:', dateFromDB )
//=> 2005-12-03T18:30:00.000Z
console.log( 'Formatted Date:', formattedDate )
//=> 03/12/05
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.1/moment.min.js"></script>
Try this
var d = new Date('2005-12-03T18:30:00.000Z');
var date = d.getDate();
var month = d.getMonth() + 1; // Since getMonth() returns month from 0-11 not 1-12
var year = d.getFullYear();
var newDate = date + "/" + month + "/" + year;
console.log("Formatted Date:", newDate)
// Formatted Date: 3/12/2005
So, that's format is called ISO 8601
'2005-12-03T18:30:00.000Z'
You can format it easily with a third-party library, I recommend to use dayjs it's lighter than moment
dayjs('2005-12-03T18:30:00.000Z').format('DD/MM/YYYY')
more info, see: https://day.js.org/docs/en/display/format
In codenameone I have a picker p1 and I want to get the values of Month, Year and day separately from the date given by the user, this code
String m1=Integer.toString(p1.getDate().getMonth());
String d1=Integer.toString(p1.getDate().getDay());
String y1=Integer.toString(p1.getDate().getYear());
generates an error when running even if there is no compilation error.
Here is how I did it
Date d = p1.getDate();
Calendar c = Calendar.getInstance();
c.setTime(d);
String m1 = Integer.toString(c.get(Calendar.MONTH) + 1);
String y1 = Integer.toString(c.get(Calendar.YEAR));
String d1 = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
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
Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
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);