使用 JavaScript 在特定日期隐藏面板
Hiding panel on certain dates using JavaScript
您好,我想在前端隐藏一个面板(用作访问注册的按钮),它应该只在一年中的特定时间可见。我正在尝试使用 DateTime 函数来获取当前日期,但它似乎对我不起作用 - 它说找不到名称 'DateTime'。任何建议表示赞赏。
这是我的代码
module PrestigeWorldWide.Scripts.ViewModels {
export class IndexViewModel extends BaseViewModels.BaseViewModel {
public panels: KnockoutObservableArray<IPanelObject> = ko.observableArray<IPanelObject>();
public events: KnockoutObservableArray<FullCalendar.EventObject> = ko.observableArray<any>();
constructor() {
super();
this.panels.push({
Name: "My Transcript",
Desc: "View your unofficial QUB transcript",
Icon: "fa-file-text",
Link: "/PrestigeWorldwide/Grade/ViewTranscript"
});
this.panels.push({
Name: "Module Info",
Desc: "View the information on all modules including pre-requisites and course content",
Icon: "fa-folder-open",
Link: "/PrestigeWorldwide/Module/ModuleInfo"
});
var cutOffStart1 = "14/09/2015";
var cutOffEnd1 = "12/10/2015";
var cutOffStart2 = "18/01/2016";
var cutOffEnd2 = "15/02/2016";
if (DateTime.Now >= cutOffStart1 && DateTime.Now >= cutOffEnd1) {
this.panels.push({
Name: "Enrollment Wizard",
Desc: "Enroll for modules and enter further information about yourself - emergency contacts etc.",
Icon: "fa-magic",
Link: "/PrestigeWorldwide/Registration/Index"
});
}
this.getEvents();
}
getEvents() {
var url = "/PrestigeWorldwide/Class/GetStudentClasses";
this.loading(true);
$.ajax(url).done((events: FullCalendar.EventObject[]) => {
this.loading(false);
_.each(events, (event) => {
this.events.push(event);
});
});
}
}
export interface IPanelObject {
Name: string;
Desc: string;
Icon: string;
Link?: string;
}
}
更新 - 我已经到了以下阶段:(有什么简单的方法可以在不使用 Moments.js 的情况下更改日期的格式?对于这么小的一段代码来说似乎有点过分了?)
//Before displaying the Registration wizard panel, check if we are in the range of registration availability dates
var cutOffStart1 = new Date(2015,09,14);
var cutOffEnd1 = new Date(2015, 10, 12);
var cutOffStart2 = new Date(2015,3,11);
var cutOffEnd2 = new Date(2015,3,15);
//var cutOffStart2 = new Date(2016,1,18);
//var cutOffEnd2 = new Date(2016,2,15);
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
//check if the current date is in the fisrt semseter registration period
if (today >= cutOffStart1 && today >= cutOffEnd1) {
this.panels.push({
Name: "Enrollment Wizard",
Desc: "Enroll for modules and enter further information about yourself - emergency contacts etc.",
Icon: "fa-magic",
Link: "/PrestigeWorldwide/Registration/Index"
});
}
//check if the current date is in the second semseter registration period
else if (today >= cutOffStart2 && today >= cutOffEnd2) {
this.panels.push({
Name: "Enrollment Wizard",
Desc: "Enroll for modules and enter further information about yourself - emergency contacts etc.",
Icon: "fa-magic",
Link: "/PrestigeWorldwide/Registration/Index"
});
}
else { };
DateTime.Now
是 c#,你在 JavaScript 文件中。
你需要做这样的事情:
var now = new Date();
还值得注意的是,您要比较的截止日期值是字符串,您也需要将它们转换为日期。
有几个js库可以操作日期,Moment.js是一个。
您好,我想在前端隐藏一个面板(用作访问注册的按钮),它应该只在一年中的特定时间可见。我正在尝试使用 DateTime 函数来获取当前日期,但它似乎对我不起作用 - 它说找不到名称 'DateTime'。任何建议表示赞赏。
这是我的代码
module PrestigeWorldWide.Scripts.ViewModels {
export class IndexViewModel extends BaseViewModels.BaseViewModel {
public panels: KnockoutObservableArray<IPanelObject> = ko.observableArray<IPanelObject>();
public events: KnockoutObservableArray<FullCalendar.EventObject> = ko.observableArray<any>();
constructor() {
super();
this.panels.push({
Name: "My Transcript",
Desc: "View your unofficial QUB transcript",
Icon: "fa-file-text",
Link: "/PrestigeWorldwide/Grade/ViewTranscript"
});
this.panels.push({
Name: "Module Info",
Desc: "View the information on all modules including pre-requisites and course content",
Icon: "fa-folder-open",
Link: "/PrestigeWorldwide/Module/ModuleInfo"
});
var cutOffStart1 = "14/09/2015";
var cutOffEnd1 = "12/10/2015";
var cutOffStart2 = "18/01/2016";
var cutOffEnd2 = "15/02/2016";
if (DateTime.Now >= cutOffStart1 && DateTime.Now >= cutOffEnd1) {
this.panels.push({
Name: "Enrollment Wizard",
Desc: "Enroll for modules and enter further information about yourself - emergency contacts etc.",
Icon: "fa-magic",
Link: "/PrestigeWorldwide/Registration/Index"
});
}
this.getEvents();
}
getEvents() {
var url = "/PrestigeWorldwide/Class/GetStudentClasses";
this.loading(true);
$.ajax(url).done((events: FullCalendar.EventObject[]) => {
this.loading(false);
_.each(events, (event) => {
this.events.push(event);
});
});
}
}
export interface IPanelObject {
Name: string;
Desc: string;
Icon: string;
Link?: string;
}
}
更新 - 我已经到了以下阶段:(有什么简单的方法可以在不使用 Moments.js 的情况下更改日期的格式?对于这么小的一段代码来说似乎有点过分了?)
//Before displaying the Registration wizard panel, check if we are in the range of registration availability dates
var cutOffStart1 = new Date(2015,09,14);
var cutOffEnd1 = new Date(2015, 10, 12);
var cutOffStart2 = new Date(2015,3,11);
var cutOffEnd2 = new Date(2015,3,15);
//var cutOffStart2 = new Date(2016,1,18);
//var cutOffEnd2 = new Date(2016,2,15);
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
//check if the current date is in the fisrt semseter registration period
if (today >= cutOffStart1 && today >= cutOffEnd1) {
this.panels.push({
Name: "Enrollment Wizard",
Desc: "Enroll for modules and enter further information about yourself - emergency contacts etc.",
Icon: "fa-magic",
Link: "/PrestigeWorldwide/Registration/Index"
});
}
//check if the current date is in the second semseter registration period
else if (today >= cutOffStart2 && today >= cutOffEnd2) {
this.panels.push({
Name: "Enrollment Wizard",
Desc: "Enroll for modules and enter further information about yourself - emergency contacts etc.",
Icon: "fa-magic",
Link: "/PrestigeWorldwide/Registration/Index"
});
}
else { };
DateTime.Now
是 c#,你在 JavaScript 文件中。
你需要做这样的事情:
var now = new Date();
还值得注意的是,您要比较的截止日期值是字符串,您也需要将它们转换为日期。
有几个js库可以操作日期,Moment.js是一个。