我搜索了很多并找到了很多解决方案,但没有一个能给我2012-12-31的正确周数.甚至MSDN(链接)上的示例都失败了.
2012-12-31是星期一,因此它应该是第1周,但我尝试的每种方法都给了我53.以下是我尝试过的一些方法:
来自MDSN图书馆:
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;
return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
Run Code Online (Sandbox Code Playgroud)
解决方案2:
return new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
Run Code Online (Sandbox Code Playgroud)
解决方案3:
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
Run Code Online (Sandbox Code Playgroud)
更新
当日期为2012-12-31时,以下方法实际返回1.换句话说,我的问题是我的方法不符合ISO-8601标准.
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be …Run Code Online (Sandbox Code Playgroud) 第二个参数parseInt()定义解析第一个参数的基数.我一直在玩一些数字,发现如果基数大于36,我就不会得到正确的答案:
parseInt("1", 36);
// -> 1
parseInt("1", 37);
// -> NaN
Run Code Online (Sandbox Code Playgroud)
有限制吗?为什么是36?
我在运行测试时使用的是chrome
我知道JavaScript中的数组只不过是一个对象.当我定义这样的数组时:
var array;
array = [ "a", "b", "c" ];
Run Code Online (Sandbox Code Playgroud)
并运行
Object.keys(array);
Run Code Online (Sandbox Code Playgroud)
我得到以下数组:["0", "1", "2"].数组长度array是3.
当我添加一个属性,如:
array["a"] = "d";
Run Code Online (Sandbox Code Playgroud)
Object.keys()正在返回["0", "1", "2", "a"],但数组长度array仍然是3.
但是当我添加这样的属性时:
array["3"] = "d";
Run Code Online (Sandbox Code Playgroud)
array现在的长度4.
如果array只是另一个对象,当我从头开始创建对象时,如何实现这种行为var myArray = {}?
我们的应用程序以 JSON 格式登录。根据 Datadog 的文档,JSON 日志不由管道处理。如何使用基于同一日志行的不同值的附加字段来丰富 JSON 日志?
我有这一行:
{"requestUri":"/customers/2934ht8/users"}
Run Code Online (Sandbox Code Playgroud)
我想要这一行:
{"requestUri":"/customers/2934ht8/users","customerId":"2934ht8"}
Run Code Online (Sandbox Code Playgroud)
Datadog 可以做到这一点吗?我不想将我们的记录器更改为customerId日志输出。
我有一个layout.jade看起来像这样:
html
body
block content
block footer
Run Code Online (Sandbox Code Playgroud)
我content.jade看起来像那样:
extends layout
block content
#Content Welcome
Run Code Online (Sandbox Code Playgroud)
我footer.jade看起来像那样:
extends layout
block footer
#Footer Impressum
Run Code Online (Sandbox Code Playgroud)
现在,当我app这样做的时候:
app.get('/', function(req, res) {
res.render('layout');
});
Run Code Online (Sandbox Code Playgroud)
我既没有看到内容也没有看到页脚.
当我跑:
app.get('/', function(req, res) {
res.render('content');
});
Run Code Online (Sandbox Code Playgroud)
然后我看到了内容.
当我跑:
app.get('/', function(req, res) {
res.render('footer');
});
Run Code Online (Sandbox Code Playgroud)
然后我看到页脚.
我怎样才能看到内容和页脚?
我从一个空页开始.如果我跑,document.body.onclick我得到null.如果我应用以下代码:
document.body.onclick = function() { return "Click"; };
Run Code Online (Sandbox Code Playgroud)
我得到的function() { return "Click"; },当我运行document.body.onclick.那讲得通!但是当我跑步的时候
document.body.addEventListener("click", function() { return "Click"; });
Run Code Online (Sandbox Code Playgroud)
document.body.onclick仍然null,但输出是"Click"我跑的时候document.body.click().
所以我的问题是,使用时存储的功能在addEventListener哪里?
假设我有以下标记:
<div id="Movie" data-genre="horror">The Hills Have Eyes</div>
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用以下方法访问该元素的数据集属性:
document.getElementById("Movie").dataset.genre
Run Code Online (Sandbox Code Playgroud)
要么
document.getElementById("Movie").getAttribute("data-genre")
Run Code Online (Sandbox Code Playgroud)
但有没有办法让所有元素具有相同的流派而不使用任何其他功能?我正在考虑这样的事情:
document.getElementsByDataSetKey(key)
Run Code Online (Sandbox Code Playgroud) 我是Promises的新手,我想知道最佳做法是在保持变量的同时保持变量?
通过Promise连接到MongoDB非常简单:
connectToMongoDB(data).done(function(db) {
var collection = db.collection('test_inserts');
// do more stuff here
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我必须连接到两个不同的数据库会发生什么?
connectToMongoDB1(data1).then(function(db1) {
return connectToMongoDB2(data2);
}).done(function(db2) {
var collection = db1.collection('test_inserts');
// ERROR: db1 is undefined
});
Run Code Online (Sandbox Code Playgroud)
这个错误非常有意义.但是如何在db1不改变我的connectToMongoDB2()功能的情况下前进,因为我想要保持connectToMongoDB2()并且我的所有承诺一般都非常通用?
我的意思是,我可以包装一个存储所有相关内容的对象,但这看起来有点像hacky:
var relevantStuff = {};
connectToMongoDB1(data1).then(function(db1) {
relevantStuff.db1 = db1;
return connectToMongoDB2(data2);
}).done(function(db2) {
var collection = relevantStuff.db1.collection('test_inserts');
// do more stuff here
});
Run Code Online (Sandbox Code Playgroud)
什么是最佳做法?
我尝试过的:
// creating elements
var container = document.createDocumentFragment();
var headline = document.createElement('h1');
headline.innerHTML = 'This is a headline.';
// attaching to DOM
container.appendChild(headline);
document.body.appendChild(container);
// attaching click event
container.addEventListener('click', function () {
console.log(arguments);
});
Run Code Online (Sandbox Code Playgroud)
此示例不起作用.事件未触发.
有没有办法将click事件附加到文档片段,或者根本不可能?
我在一本书中读到这篇文章,只是想知道它是如何工作的,如果你做过类似的事情,你会做什么.我知道你可以回来,(x>y)但你为什么要这样做false == (x > y)?
我运行我的节点应用程序localhost:3000,它正在为路由提供默认页面/.如果我访问http://localhost:3000默认页面则相应显示.我还运行了一个基本配置如下的Nginx服务器:
server {
listen 80;
server_name localhost;
location /node_app {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我http://localhost/node_app现在运行,我的节点应用程序会抛出一个错误,说它找不到路由/node_app.
如何以我可以通过调用访问应用程序的方式配置我的节点应用程序或nginx服务器http://localhost/node_app,但应用程序本身认为它在/?
如果我添加/到http://127.0.0.1:3000它实际上是匹配/node_app的/路线.但现在,默认页面中的每个样式表现在指向错误的路径.
我想要实现的是解析给定的网站并将其标题写入titles.txt.我正在使用请求模块获取网站和cheerio获取标题.
我正在使用Q模块创建以下两个承诺:
readTitle
var readTitle = function(url) {
var deferred = Q.defer();
request({
url: url
}, function(err, response, body) {
var $ = cheerio.load(body);
deferred.resolve($("title").text());
});
return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)
writeTitle
var writeTitle = function(title) {
var deferred = Q.defer();
fs.appendFile('titles.txt', title + "\n", function() {
deferred.resolve();
});
return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)
我期待下面的脚本编写Google和Stack Overflow文本文件:
readTitle('http://www.google.com')
.then(writeTitle)
.then(readTitle('http://www.stackoverflow.com'))
.then(writeTitle);
Run Code Online (Sandbox Code Playgroud)
但我真正得到的是:
Google
undefined
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?