JavaScript将单个值存储为日期格式
我正在开发一个“租车”网络应用程序。。。所以,用户可以选择日期“从”和“直到”他想租一辆车。。这些值存储在单个变量中(见下文): 所以,因为车主注意到在一年中的某些时期需求较高,所以在这一时期(季节),他想提高租车价格 所以,在以下日期之间,价格应该上涨 saison1 1.3.17-21.5.17价格上涨15% 赛森2 19.06.17-02.07.17价格上涨10% saison3 12.09.17-31.10.17价格上涨10% 我的问题是如何将日期存储到变量中,以便编写条件语句 例如:JavaScript将单个值存储为日期格式,javascript,date,conditional,Javascript,Date,Conditional,我正在开发一个“租车”网络应用程序。。。所以,用户可以选择日期“从”和“直到”他想租一辆车。。这些值存储在单个变量中(见下文): 所以,因为车主注意到在一年中的某些时期需求较高,所以在这一时期(季节),他想提高租车价格 所以,在以下日期之间,价格应该上涨 saison1 1.3.17-21.5.17价格上涨15% 赛森2 19.06.17-02.07.17价格上涨10% saison3 12.09.17-31.10.17价格上涨10% 我的问题是如何将日期存储到变量中,以便编写条件语句 例如:
if(date == saison1) {
function increaseThePrice() {
newPrice = price*1.5;
return newPrice;
}
我希望我的问题足够清楚
提前谢谢
Denis通过客户端脚本设置价格相关信息可能很危险
如果您通过JavaScript设置折扣,用户可以简单地修改它并下订单,而不是15%,比如说95%。所以要小心。使用服务器脚本设置价格和折扣不使用JavaScript日期只不过是一个格式化的字符串,因此可以将其转换为日期对象、格式自定义日期等。这意味着您可以将其存储为普通字符串,但需要使用已知的日期格式
var date = "2016/02/12"; // for example
但是,使用javascript将折扣日期存储在客户的浏览器中可以让他完全控制您的javascript数据,因此它很容易被操纵和滥用。你绝对应该使用一些后端 我相信@Softberry是对的,你不应该把这个放在客户端 我合并了@RobG建议的更改。( “…此解决方案中构造的所有日期均为GMT+0…”仅当该日期是运行脚本的主机的时区时,或者如果要使用新日期(Date.UTC(2017,9-1,17))。无需提供零值,缺少的参数默认情况下被视为零(日期除外,默认为1) 现在,如果您在客户端或服务器上使用它,问题仍然是一样的 下面是一个javascript解决方案
/** main logic */
function getCostForInterval(basePrice,from,back){
var total=0;
var seasonAdjustments=getSeasonPriceMultipliers();
/* iterate through each day of the interval (from-back)*/
for(
var d=from;/* start */
d.getTime()<=back.getTime();/* end condition */
d.setDate(d.getDate()+1)/* increment by one day */
){
total+=basePrice*getAdjustmentForDate(d,seasonAdjustments);
}
return total;
}
/** getAdjustmentForDate returns either 1 or
* or the corresponding price multiplier
* when the date falls into a season
*/
function getAdjustmentForDate(date,seasons){
var multiplier=1;
for(var ix=0;ix<seasons.length;ix++){
var season=seasons[i];
/** if date falls inside current season
**/
if(season.begin.getTime()<date.getTime()
&& date.getTime()<season.begin.getTime() ){
return season.multiplier;
}
}
/** if date doesn't fall inside any
* of the season intervals */
return multiplier;
}
/*** getSeasonPriceMultipliers
** returns an array of intervals where
** price modifications may apply
**
** you can change the data retrieving mechanism
** from hard coded to an http/ajax call
***/
function getSeasonPriceMultipliers(){
var saisons=[
{
begin : new Date(Date.UTC(2017,3-1,1,0,0,0)),
end : new Date(Date.UTC(2017,5-1,21,23,59,59)),
multiplier : 1.15
},
{
begin : new Date(Date.UTC(2017,6-1,19,0,0,0)),
end : new Date(Date.UTC(017,7-1,2,23,59,59)),
multiplier : 1.10
},
{
begin : new Date(Date.UTC(2017,9-1,17,0,0,0)),
end : new Date(Date.UTC(2017,10-1,31,23,59,59)),
multiplier : 1.10
}
];
return saisons;
}
此解决方案不考虑时区,此解决方案中构造的所有日期均为GMT+0。
请注意检索点和返回点之间的时区差异。如果价格是每天的,则夏令班不会造成问题,如果价格是每小时的,则需要相应调整
如果您在客户端使用javascript,我建议将整个过程包装在IIFE中,只公开函数getCostForInterval,使用http调用在函数getSeasonPriceMultipliers中检索季节数据,并最终丑化整个过程(source和json)您不使用服务器吗?当您可以轻松地将日期存储在服务器上的数据库中时,为什么要将日期存储在变量中?当您需要时,从服务器获取季节日期,并将其与当前日期进行比较。“…此解决方案中构造的所有日期都是GMT+0…”仅当该时区是运行脚本的主机的时区时,或者如果要使用
新日期(Date.UTC(2017,9-1,17))
。无需提供零值,缺少的参数默认为零(日期除外,默认为1).非常感谢你的努力!非常感谢你的努力!非常感谢你的努力!
/** main logic */
function getCostForInterval(basePrice,from,back){
var total=0;
var seasonAdjustments=getSeasonPriceMultipliers();
/* iterate through each day of the interval (from-back)*/
for(
var d=from;/* start */
d.getTime()<=back.getTime();/* end condition */
d.setDate(d.getDate()+1)/* increment by one day */
){
total+=basePrice*getAdjustmentForDate(d,seasonAdjustments);
}
return total;
}
/** getAdjustmentForDate returns either 1 or
* or the corresponding price multiplier
* when the date falls into a season
*/
function getAdjustmentForDate(date,seasons){
var multiplier=1;
for(var ix=0;ix<seasons.length;ix++){
var season=seasons[i];
/** if date falls inside current season
**/
if(season.begin.getTime()<date.getTime()
&& date.getTime()<season.begin.getTime() ){
return season.multiplier;
}
}
/** if date doesn't fall inside any
* of the season intervals */
return multiplier;
}
/*** getSeasonPriceMultipliers
** returns an array of intervals where
** price modifications may apply
**
** you can change the data retrieving mechanism
** from hard coded to an http/ajax call
***/
function getSeasonPriceMultipliers(){
var saisons=[
{
begin : new Date(Date.UTC(2017,3-1,1,0,0,0)),
end : new Date(Date.UTC(2017,5-1,21,23,59,59)),
multiplier : 1.15
},
{
begin : new Date(Date.UTC(2017,6-1,19,0,0,0)),
end : new Date(Date.UTC(017,7-1,2,23,59,59)),
multiplier : 1.10
},
{
begin : new Date(Date.UTC(2017,9-1,17,0,0,0)),
end : new Date(Date.UTC(2017,10-1,31,23,59,59)),
multiplier : 1.10
}
];
return saisons;
}
function test(BASE_PRICE,backDay,backMonth,backYear, backHours,backMinutes,
fromDay,fromMonth,fromYear,fromHours,fromMinutes
){
var from=new Date(fromYear,fromMonth,fromDay,fromHours,fromMinutes,0)
var back=new Date(backYear,backMonth,backDay,backHours,backMinutes,0)
return getCostForInterval(BASE_PRICE,from,back)
}