MeteorJS: Template Helpers [duplicate] - meteor

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>

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.

Compare Dates in Angular 2 [duplicate]

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.

Bootstrap-datepicker and multidate values in input field

I use Bootstrap-datepicker with embedded and multidate options.
I would like that chosen dates appear in the input field. How to do this?
I try this code but only one date appear :
$('#date').datepicker().on('changeDate', function(e){
$('#date_input').val(e.format('mm/dd/yyyy'));
});
With this other solution, the date format is not convenient for me :
$('#date').datepicker().on('changeDate',function(){
var value=$("#date").datepicker("getDates");
$('#date_input').val(value);
});
I also love the format is as follows: dd/mm/yyyy
For instance : 06/07/2014, 07/07/2014, 10/07/2014
Can you help me please?
In advance thanks a lot for your answers
var picker = $('#picker').datepicker();
console.log( picker.datepicker('getFormattedDate') );

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

Javascript Date Localization

I'm working with an ASP.NET app with localization and globalization. I'm having some difficulty understanding how to get the Date() function in javascript to work properly given the user's environment. My user base is split between Mexico (spanish) and the US (english). Since the Mexico date format is dd/mm/yyyy and the english format is mm/dd/yyyy, the standard Date(strDate) javascript constructor does not work for me.
Does anyone know the best way to handle globalization/localization of a javascript Date value? I have some business rules to enforce like dateA must be 90 days prior to dateB and dateB cannot exceed today.
Take a look at datejs, it handles localization very nicely. It comes with a lot of globalization setups. You just load the globalization setup of your current CultureInfo and datejs takes care of the rest.
Matt Kruse developed a really interesting date library which should help with your particular case.
Here's a snippet of the method you should use for the issue you mentioned:
// ------------------------------------------------------------------
// parseDate( date_string [, prefer_euro_format] )
//
// This function takes a date string and tries to match it to a
// number of possible date formats to get the value. It will try to
// match against the following international formats, in this order:
// y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
// M/d/y M-d-y M.d.y MMM-d M/d M-d
// d/M/y d-M-y d.M.y d-MMM d/M d-M
// A second argument may be passed to instruct the method to search
// for formats like d/M/y (european format) before M/d/y (American).
// Returns a Date object or null if no patterns match.
// ------------------------------------------------------------------
function parseDate(val) {
var preferEuro=(arguments.length==2)?arguments[1]:false;
generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d');
monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d');
dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M');
var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst');
var d=null;
for (var i=0; i<checkList.length; i++) {
var l=window[checkList[i]];
for (var j=0; j<l.length; j++) {
d=getDateFromFormat(val,l[j]);
if (d!=0) { return new Date(d); }
}
}
return null;
}
You could use: var a = Date.parseLocale(value, formats);
If you provide no custom formats, this function uses the Sys.CultureInfo.CurrentCulture property to determine the culture value.
You can take a look on: http://msdn.microsoft.com/en-us/library/bb397521.aspx
I wrote an answer to this here. It uses the toLocalString to determine MM/DD/YYY, DD/MM/YYYY,...
https://stackoverflow.com/a/18154195/119741

Resources