如何使用特定格式格式化字符串变量
How to format string variable to date with specific format?
我的案子需要帮助。我是JS的新手。我从当前页面获得值(10/19/2016),并尝试创建Date对象。但如果日期是(19/10/2016),它会给我一个NaN页面。我需要这样的格式(MyVar,"dd/mm/yy")在任何时候的变量。怎么可能呢,我被这件事难住了。
<link href="{!$Resource.fullCalendarCSS}" rel="stylesheet" />
<link href="{!$Resource.fullCalendarPrintCSS}" rel="stylesheet" media="print" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="//code.jquery.com/jquery-1.8.3.js"></script>
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script src="{!$Resource.JqueryDateFormatJS}"></script>
<script src="{!$Resource.JqueryDateFormatMinJS}"></script>
<script src="{!$Resource.DateFormatJS}"></script>
<script src="{!$Resource.DateFormatMinJS}"></script>
<script src="{!$Resource.fullCalendarMinJS}"></script>
<script type='text/javascript'>
$.noConflict();
jQuery(document).ready(function() {
tempValue = '{!$CurrentPage.parameters.startDate}';
newDate1 = $.datepicker.formatDate("mm/dd/yy", new Date(tempValue));
console.log(newDate1);
newDate = new Date(newDate1);
console.log(newDate);
d = newDate.getDate();
m = newDate.getMonth();
y = newDate.getFullYear();
//We need to wrap everything in a doc.ready function so that the code fires after the DOM is loaded
//Call the fullCallendar method. You can replace the '#calendar' with the ID of the dom element where you want the calendar to go.
jQuery('#calendar').fullCalendar({
year: y,
month: m,
date: d,
defaultView: 'agendaDay',
slotMinutes: 15,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: false,
events:
[
//At run time, this APEX Repeat will reneder the array elements for the events array
<apex:repeat value="{!events}" var="e">
{
title: "{!e.title}",
start: '{!e.startString}',
end: '{!e.endString}',
url: '{!e.url}',
allDay: {!e.allDay},
className: '{!e.className}',
},
</apex:repeat>
]
});
});
</script>
我使用fullCalendar和DateFormat插件。
如果我的变量tempValue的格式为"mm/dd/yy",我可以这样定义日期对象:
date = new Date(tempVal) ----> Thu Oct 20 2016 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)
,如果vy变量的格式是"dd/mm/yy",它会给我错误的"Invalid date"。
我需要得到tempValue只在格式"mm/dd/yy",即使它的格式"dd/mm/yy"。
Javascript Date()
期望一些日期字符串格式…
但不是dd/mm/yyyy。
你注意到了。
那么,既然已经有了正确的日期选择器,为什么不简单地用.split
来检索部件呢?
如果你想对日期进行计算,比如找出两个日期之间的差异,那么使用这些拆分部分将正确的格式传递给Date()
。
// Commented out because we don't have this value here in this snippet.
//tempValue = '{!$CurrentPage.parameters.startDate}';
// I used this date value instead...
tempValue = "24/09/2016";
console.log(tempValue);
//newDate1 = $.datepicker.formatDate("mm/dd/yy", new Date(tempValue));
//console.log(newDate1);
//newDate = new Date(newDate1);
// Get the splitted values
var dateTemp = tempValue.split("/");
d = dateTemp[0];
m = dateTemp[1];
y = dateTemp[2];
console.log("d: " + d);
console.log("m: " + m);
console.log("y: " + y);
// To perform calculations, you'll need this.
calcDate1 = new Date(m + "/" + d + "/" + y);
console.log(calcDate1.toString());
Just run the snippet and check the console...<br>
;)
所以最后我找到了解决方案…确切的问题是在User区域设置中,因此区域设置为English (united states)的用户具有'M/d/yyyy'日期格式,因此我们需要编写代码来为每种区域设置日期格式创建date对象。
最后:
<script type='text/javascript'>
$.noConflict();
jQuery(document).ready(function() {
tempValue = '{!$CurrentPage.parameters.startDate}';
var userLang = UserContext.dateFormat;
var dateTemp = tempValue.split("/");
d1 = dateTemp[0];
m1 = dateTemp[1];
y1 = dateTemp[2];
y='';
d='';
m='';
console.log(userLang);
if (userLang === "M/d/yyyy") {
newDate = new Date(d1 + "/" + m1 + "/" + y1);
d = newDate.getDate();
m = newDate.getMonth();
y = newDate.getFullYear();
};
if (userLang === "dd/MM/yyyy") {
newDate = new Date(m1 + "/" + d1 + "/" + y1);
d = newDate.getDate();
m = newDate.getMonth();
y = newDate.getFullYear();
};
//We need to wrap everything in a doc.ready function so that the code fires after the DOM is loaded
//Call the fullCallendar method. You can replace the '#calendar' with the ID of the dom element where you want the calendar to go.
jQuery('#calendar').fullCalendar({
year: y,
month: m,
date: d,
defaultView: 'agendaDay',
slotMinutes: 15,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: false,
events:
[
//At run time, this APEX Repeat will reneder the array elements for the events array
<apex:repeat value="{!events}" var="e">
{
title: "{!e.title}",
start: '{!e.startString}',
end: '{!e.endString}',
url: '{!e.url}',
allDay: {!e.allDay},
className: '{!e.className}',
},
</apex:repeat>
]
});
});
</script>
我认为这不是最好的解决方案,但如果你有什么主意,请告诉我。
谢谢!
相关文章:
- 如何格式化字符串以修复Javascript中的缩进
- momentjs 显示本地时间和自动格式化字符串
- 有没有办法在javascript中格式化字符串
- Javascript/Regex,用于在键入时格式化字符串
- 如何在 Response.Write 中格式化字符串
- 我是jQuery和JSON的新手.我需要帮助格式化字符串,以便我可以迭代它
- 如何在 JavaScript 中获取格式化字符串的值
- 在 javascript 中格式化字符串
- 从ISODate转到一个漂亮的格式化字符串
- 使用javascript jquery或css格式化字符串日期
- Regex用于查找Java's在JavaScript中格式化字符串
- 如何使用格式化字符串访问JavaScript对象中的SUB属性
- 谷歌地图,格式化字符串.(去掉撇号)
- 通过getJSON返回函数获取格式化字符串
- 计算字符串中的出现次数和格式化字符串:显示结果
- Javascript将YYMMDD格式化字符串转换为日期对象
- RegEx在JavaScript中拆分格式化字符串
- 如何使用特定格式格式化字符串变量
- 如何在javascript中使用正则表达式格式化字符串
- 在JS中格式化字符串到日期