Compare Dates in Angular 2 [duplicate] - datetime

This question already has answers here:
JavaScript Date Object Comparison
(7 answers)
Closed 6 years ago.
I'm trying to figure out how best to compare dates in Angular 2 and TypeScript 1.8.7.
Given:
startDate: Date;
endDate: Date;
if(this.startDate.getUTCMilliseconds() === this.endDate.getUTCMilliseconds()){
//do stuff here
} else {
// do something else here
}
This will return an error like "startDate.getUTCMilliseconds is not a function..."
Does somebody have a best practice? Thanks,

Date object method to get milliseconds is called getTime.

You can compare Date objects directly, with operators like <, >, and ==.
this.startDate == this.endDate

I know it's irrelevant and not what you asked for, but use angular-moment / momentjs. Dates in JS are a complete mess.

Related

must declare the scalar variable "#nInstID" [duplicate]

This question already has answers here:
Must declare the scalar variable
(11 answers)
must declare the scalar variable '#custid' using dbcontext.Database.SqlQuery?
(2 answers)
Closed 4 years ago.
I am using a ADO.NET Entity Data Model for accessing my tables and stored procedure. I mapped stored procedure to my Room table as it returning sfloor column. In Entity Framework, my get request method for floor is:
public List<Floor> GetFloorList(long instID,long userID,string userRole, long buildID, long deptID)
{
try
{
var floors = dbdata.Database.SqlQuery<Room>("GetUserFloorList #nInstID, #nUserID, #sUserRole, #nBuildID, #nDeptID", instID, userID, userRole, buildID, deptID);
List<Floor> floorList = new List<Floor>();
foreach (var fl in floors)
{
Floor floor = new Floor();
floor.floorName = fl.sFloor;
floorList.Add(floor);
}
return floorList;
}
catch(Exception)
{
throw;
}
}
As I want to list out all the floor name which belongs to corresponding parameters. But when I am running this method it giving me above error.
As per some solution I need to mention data types of each variable but I am not sure how to do that. Can anyone tell me what to do?
Thanks in advance.
Instead of passing parameters with your variable directly, you have to use new SqlParameter("#nInstID", instID) and do this for all your parameters.

Using an if else statement in R? [duplicate]

This question already has answers here:
Nested ifelse statement
(10 answers)
Closed 5 years ago.
Write a function called dayplan(temperature,work). The Function should at first check if it is dealing with realistic values. Thereby temperature∈[-20,40] and work∈{´Yes´, ´No´}. For realistic values the function should give out these values:
When temperature>=20 and job==´Yes´, then "Eat ice cream"
When temperature>=20 and job==´No´, then "Sea"
When temperature >20 and job ==´Yes´, then "Shopping"
When temperature<20 and job==´No´, then "Bed"
The outline of the function can look something like this:
dayplan <- function(temperature, work){
if(...){
if(...){
print("Eat ice cream.")
} else if(...) {
...
...
}
} else {
print("Inputs not feasible.")
}
}
Fill in the gaps using the conditions for each activity.

MeteorJS: Template Helpers [duplicate]

This question already has answers here:
How to format date in meteor template
(3 answers)
Closed 7 years ago.
Have some problem with "Where transform data from MongoDB"
I want to show date like 10 JUL (for this i must use moment().format(MMM D)) i use momentjs
But in my Mongo i have date in basic format 2015-07-.... ( moment().format())
Where its output
<td class="submitDate ">{{date}}</td>
I want to do something like this
Template.postJobs.helpers({
date: function(){
return this.format("MMM D");
}
});
QUESTION : is WHERE i can transform date from 2015-07-.... ( moment().format()) to 10 JUL``moment().format(MMM D) and HOW
Anybody help ??
SOLVED:
<td class="submitDate ">{{formatTime date}}</td>
UI.registerHelper('formatTime', function(context, options) {
if(context)
return moment(context).format('MMM D');
});
You can create a helper formatter method to do the formatting:
Template.postJobs.helpers({
formatDate: function(date) {
return date.format("MMM D");
}
});
Your html template would pass the date variable into formatDate like this:
<td class="submitDate">{{formatDate this}}</td>

Regex verification correct birth date and check age

I need a regex which takes the string YYYY-MM-DD-XXXX (The last 4 are just for purpose of gender/area) It's mostly important to check the first 8 Digits for a valid birth date.
So far i have this:
/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})\-([0-9]{4})$/
Also i want to check so the input age is at least 18 years old. Would appreciate if somone had some input on how to achieve this.
Edit: The regex above was tested in JS, but should work fine in ASP as well?
I have changed your regex a bit to make it look more authentic
^([1-2]\d{3})\-([0-1][1-9])\-([0-3][0-9])\-([0-9]{4})$
years like 3012 will not pass.
Now you want to find whether a person is 18 years or not.
One approach could be to find the difference between the years of dates provided like this
var str = '1990-09-12-5555';
var res = /^([1-2]\d{3})\-([0-1][1-9])\-([0-3][0-9])\-([0-9]{4})$/.exec(str);
var year_now = new Date().getFullYear();
console.log(year_now-res[1]);
a second approach will be more precise one :
var str = '1990-09-12-5555';
var res = /^([1-2]\d{3})\-([0-1][1-9])\-([0-3][0-9])\-([0-9]{4})$/.exec(str);
var todays_date = new Date();
var birth_date = new Date(res[1],res[2],res[3]);
console.log(todays_date-birth_date);
will output the result in milliseconds. You can do the math to convert it into year
Cheers , Hope that helps !
I suggest using moment.js which provides an easy to use method for doing this.
interactive demo
function validate(date){
var eighteenYearsAgo = moment().subtract("years", 18);
var birthday = moment(date);
if (!birthday.isValid()) {
return "invalid date";
}
else if (eighteenYearsAgo.isAfter(birthday)) {
return "okay, you're good";
}
else {
return "sorry, no";
}
}
To include moment in your page, you can use CDNJS:
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>
Source
The following will match any year with a valid day/month combination, but won't do validation such as checking you've not entered 31 days for February.
^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])\-[0-9]{4}$
Not sure exactly what you're trying to achieve but I'd suggest using a date library for this sort of thing. You could return a message to the user somehow if the entered date fails to parse into an object.
In order to do age validation, you will certainly need to use a library so a regex should only be used for date validation purposes

How to add one month in current month?

Suppose the current month is "Oct". I want that it will add one month in current month i.e it will show "Nov". For this my code is written below but it gives exception that Input string was not in a correct format.
So please correct the code?
if (Convert.ToInt32(ddlMonth.SelectedIndex ) <= Convert.ToInt32(DateTime.Now.AddMonths(1).ToString()))
{
TotalBalanceUptoSelectedPreviousMonth();
}
This should probably work:
if (Convert.ToInt32(ddlMonth.SelectedIndex )
<= Convert.ToInt32(DateTime.Now.AddMonths(1).ToString("M")))
TotalBalanceUptoSelectedPreviousMonth();
However, it looks simpler like this:
if (Convert.ToInt32(ddlMonth.SelectedIndex) <= DateTime.Now.AddMonths(1).Month)
Total...();
Convert.ToInt32(DateTime.Now.AddMonths(1).ToString())
probably gives the exception since DateTime.Now.AddMonths(1).ToString() value is not castable to Int32.
I'll go out on a limb and say that this is what you're looking for:
if(ddlMonth.SelectedIndex <= DateTime.Now.AddMonths(1).Month)
{
TotalBalanceUptoSelectedPreviousMonth();
}
Instead of getting the date as a string and converting it, why not use the Month property of the DateTime struct (which is already an integer)?
(Oh...and SelectedIndex is already an integer as well, no need for the call to Convert)
You need to use DateTime.Now.AddMonths(1).Month.
Also you do not need to use the ToString method wrapped in a Convert.ToInt32 method as this is already an integer.

Resources