使用MomentJS,如果需要,我可以使用.locale(或.lang在旧版本中)在特定时刻实例(而不是全局)上设置区域设置.如何使用除全局区域之外的特定区域设置解析字符串?由于moment函数本身就是我们用于解析的东西,并且似乎.set没有完整解析的变体?
例如:
// Assume the default locale is 'en' (just in case, I'll set it for the snippet)
moment.locale('en');
// I have this string that's in the French locale
var str = "30 Avril 2015";
// Parsing it fails
var m = moment(str, "DD MMMM YYYY");
snippet.log(m.format("YYYY-MM-DD")); // "Invalid Date"Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>Run Code Online (Sandbox Code Playgroud)
我从api xml数据获取,其中一个xml元素是日期时间,nodeValue始终采用这种格式 - 字符串:"YYYY-MM-DD".(我无法从api请求,以不同格式返回日期时间)
我的问题是将此格式拆分并转换为此字符串:"DD.MM.YYYY"
基本上我这样做了:
var myString = "2015-04-10"; //xml nodeValue from time element
var array = new Array();
//split string and store it into array
array = myString.split('-');
//from array concatenate into new date string format: "DD.MM.YYYY"
var newDate = (array[2] + "." + array[1] + "." + array[0]);
console.log(newDate);
Run Code Online (Sandbox Code Playgroud)
这是jsfiddle:http://jsfiddle.net/wyxvbywf/
现在,这段代码可行,但我的问题是:有没有办法以更少的步骤获得相同的结果?
var metricUnits = function(num) {
console.log(num.toString().length);
};
// This works, gives me 21
metricUnits(900000000000000000000);
// But this fails, gives me 5
metricUnits(9000000000000000000000);
Run Code Online (Sandbox Code Playgroud)
当我调用此函数时,21将记录到控制台.但是,当我在输入参数的末尾添加一个或多个零时,将5打印到控制台?!为什么是这样?
我读到只做& 0x7fffffff符号位的掩码,并没有篡改其他位.
int a = Integer.MIN_VALUE;
System.out.println(a & 0x7fffffff);
Run Code Online (Sandbox Code Playgroud)
但是,这段代码输出
0
Run Code Online (Sandbox Code Playgroud)
代替
2147483648
Run Code Online (Sandbox Code Playgroud)
这是为什么?
一个PHP脚本正在计算我以下数组:
$test = Array();
$test['a'] = Array();
$test['a']['a1'] = 'a1';
$test['a']['a2'] = 'a2';
$test['b'] = Array();
$test['b']['b1'] = 'b1';
$test['b']['b2'] = 'b2';
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法将此数组转换为JSON:
echo json_encode($test);
Run Code Online (Sandbox Code Playgroud)
我正在使用Ajax调用检索此JSON,我正在使用以下方法将其转换为JavaScript数组:
test = JSON.parse(data);
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何在JavaScript中向此数组添加条目?我试过了:
test['c'] = [];
test['c']['c1'] = 'c1';
test['c']['c2'] = 'c2';
Run Code Online (Sandbox Code Playgroud)
但是然后在控制台中test['c']是空的(Array[0]).
下面的脚本将替换实际图像对象的所有图像标记.不幸的是,我收到了一个Uncaught TypeError错误.这是因为在我的示例中,Javascript从0计数到3,而不是1到4.数组的长度为4.
如何避免此错误(因此我也在while循环后获得控制台日志)?
var raw_content = {
"text" : "Test image 1 [image] Test image 2 [image] Test image 3 [image] Test image 4 [image]",
"media" :[
{
"src": "http://placehold.it/400x200",
"caption": "This is a caption"
},{
"src": "images/gallery/sh0.png",
"caption": "This is a caption"
},{
"src": "http://placehold.it/400x200",
"caption": "This is a caption"
},{
"src": "images/gallery/sh0.png",
"caption": "This is a caption"
}
]
};
// find img one by one
var image_counter = 0;
var text_buffer = raw_content.text; …Run Code Online (Sandbox Code Playgroud) 我在nodejs上运行此代码.我想知道为什么闭包执行时不打印字符串'Globals'?是不是this在关闭指向全球范围?
// Running on NodeJS, not in a browser!
this.name = "Globals";
function Person(name) {
this.name = name;
this.namePrinter = function() {
return function() {
console.log(this.name);
}
}
}
var p = new Person("Faiz");
p.namePrinter()(); // prints undefined. Shouldn't it print Globals?
console.log(this.name); // prints Globals
Run Code Online (Sandbox Code Playgroud) 在命令行界面中编译应用程序时,我有时会看到如下编写的命令:
javac -d . HelloWorld.java
Run Code Online (Sandbox Code Playgroud)
我明白那个:
-d <directory> =指定生成的类文件的放置位置. =当前文件夹我的问题是:写作的目的是什么-d .?
它似乎是自我指责的,完全是多余的/不必要的.我希望只是以下内容,据我所知,它具有相同的效果并且不那么冗长:
javac HelloWorld.java
Run Code Online (Sandbox Code Playgroud)
有什么东西我错过了吗?
我使用symbolhound.com在网上搜索这个特定的短语,但找不到任何解释.
Oracle Java站点上的此页面可以执行此操作,例如:
javac -d . XorInputStream.java javac -d . XorOutputStream.java javac -d . XorSocket.java javac -d . XorServerSocket.java javac -d . XorServerSocketFactory.java javac -d . XorClientSocketFactory.java javac -d . Hello.java javac -d . HelloClient.java javac -d . HelloImpl.java
我对使用 React 真的很陌生,我使用 IntelliJ 作为我的 IDE。
我试图在我的网站上显示 JSON 数据文件的内容,但我收到错误消息,说我创建了非法导入减速。
import PostData from "./JSONData.js";
Run Code Online (Sandbox Code Playgroud)
我在类中加载信息的类如下
class Invoice extends React.Component
{
render()
{
return (
<div className ="container">
<h2>Allstate NI Graduate Application</h2>
<h2>Invoice</h2>
{PostData.map((postDetail, index)=>{
return <h1>{postDetail.code}</h1>
})}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要 HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gerry Byrne and David Wilson - ReactJS Tutorials</title>
<img src="../Drawable/carImage.png" id="carImg">
<!--Bootstap CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4"
crossorigin="anonymous">
<!-- Tells the file to get the react.js file from a CDN.-->
<script …Run Code Online (Sandbox Code Playgroud) 在编写 jsx/tsx 代码、弹出并优先于我真正想要的方法或其他完成时,Emmet 缩写通常会妨碍我。例如,在此屏幕截图中,Emmet 缩写妨碍了自动完成replace字符串上的方法:
这种情况出人意料地频繁发生。
我目前不使用 Emmet。有没有办法完全将其关闭,或者至少.js,.jsx,.ts,和.tsx文件?我已经对设置进行了一些调整,虽然有很多与 Emmet 相关的选项,但我找不到一个简单的关闭开关!:-)