格式化字符串 [英] Formatting strings

查看:88
本文介绍了格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JS的新手。我想将日期和时间插入到

文档中,格式为:


WB-MMDDHHmm


其中:


WB-是一个固定的字符串前缀(整个字符串是一个参考号)

MM是01到12个月,领先0如果需要

DD是01到31天,如果需要,前导0

HH是00到23小时,如果需要,前导0

mm是分钟00到59,如果需要,前导0

我已经走到这一步了:

< script language =" JavaScript" ;>


函数ShowDateTime()

{

var today = new Date();

var mo = today.getMonth()+ 1;

var da = today.getDate();

var ho = today.getHours();

var mi = today.getMinutes();


#解析这里#


document.write(" WB- + mo + da + ho + mi);

}


< / script>

问题是有点解析如果(mo.length<),我已经尝试了


2)mo =0+ mo; ......等等


但它不起作用。


帮助!

-

Nige


请用当年替换YYYY

非法quis mortem cum maximus ludos,vincat

I''m a complete novice to JS. I want to insert the date and time into a
document in the format:

WB-MMDDHHmm

Where:

WB- is a fixed string prefix (the whole string is a reference number)
MM is the month 01 to 12, with a leading 0 if required
DD is the day 01 to 31, with a leading 0 if required
HH is the hour 00 to 23, with a leading 0 if required
mm is the minute 00 to 59, with a leading 0 if required
I''ve got this far:
<script language="JavaScript">

function ShowDateTime()
{
var today = new Date();
var mo=today.getMonth()+1;
var da=today.getDate();
var ho=today.getHours();
var mi=today.getMinutes();

# parse here #

document.write("WB-"+mo+da+ho+mi);
}

</script>
The problem is the bit to parse the strings, I''ve tried

if (mo.length < 2) mo="0"+mo; ... etc

but it doesn''t work.

Help!
--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat

推荐答案

Nige写道:
Nige wrote:
function ShowDateTime()
{
var today = new Date();
var mo = today.getMonth()+ 1;
var da = today.getDate();
var ho = today.getHours();
var mi = today.getMinutes ();

#parse here#
document.write(" WB-" + mo + da + ho + mi);
}
[...]

问题是要解析字符串,我已经尝试了如果(mo.length< 2)mo =" 0 QUOT + MO; ......等等

但是它不起作用。
function ShowDateTime()
{
var today = new Date();
var mo=today.getMonth()+1;
var da=today.getDate();
var ho=today.getHours();
var mi=today.getMinutes();

# parse here #

document.write("WB-"+mo+da+ho+mi);
}
[...]

The problem is the bit to parse the strings, I''ve tried

if (mo.length < 2) mo="0"+mo; ... etc

but it doesn''t work.




Date.get ...(...)方法返回数字类型的值。当你将它们分配给变量并使用查找运算符`。''与该变量一起使用时,存储在该变量中的

值将转换为Number对象。 br />
不幸的是,这些对象没有'length''属性,所以值

是'undefined''。但是`undefined< 2''等于'false''(因为你可以而且应该使用alert(...)或document.write(...)测试
)所以第二次分配给

`mo''aso。没有执行。


我看到两种可能的方法来解决问题:


A)将数字转换为字符串。 String对象有一个

长度的属性,你可以比较:


if(String(mo).length< 2)

...


B)你需要的只是一个两位数的前导零。

所以只有在需要的时候连接它,因为这个数字比10美元还要少b / b:

如果(mo <10)

......


我使用并建议使用B)因为它更快并分配更少的内存。

HTH


PointedEars


在comp.lang.javascript中,Thomas''PointedEars''Lahn写道:
In comp.lang.javascript, Thomas ''PointedEars'' Lahn wrote:
Date.get .. 。(...)方法返回类型为`number''的值。
The Date.get...(...) methods return values of type `number''.




当然,谢谢。


我已经意识到这个问题比我想象的要复杂得多。

计划是在用户提交表格时生成一个由日期和时间组成的参考号




引用号是由处理

表单的CGI脚本创建的,但(当然)如果用户的时钟与服务器的时钟不同

那么数字是不同的。 D''OH!


当用户按下提交按钮时,有没有办法生成一个引用当前日期的字符串和

时间(以MMDDHHmm为单位) ,发送这个

字符串与其他表格数据,然后在页面上显示这个字符串

,接下来显示?


-

Nige


请用当年替换YYYY

非法quis mortem cum maximus ludos,vincat



Of course, thank you.

I''ve since realized that the problem is more complex than I thought. The
plan was to generate a reference number consisting of the date and time
when a user submits a form.

The reference number is created by the CGI script that processes the
form, but (of course) if the user''s clock is not the same as the server
then the numbers are different. D''OH!

Is there a way to generate a string referencing the current date and
time (as MMDDHHmm) when a user presses the Submit button, send this
string with the other form data, then display this string on the page
that is displayed next?

--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat


Nige写道:
Nige wrote:
我已经意识到这个问题比我想象的要复杂得多。
计划是在用户提交表单时生成由日期和时间组成的参考编号。

参考编号由处理<的CGI脚本创建。 br />形式,但(当然)如果用户的时钟与服务器的时钟不同,那么数字就不同了。 D''OH!
I''ve since realized that the problem is more complex than I thought. The
plan was to generate a reference number consisting of the date and time
when a user submits a form.

The reference number is created by the CGI script that processes the
form, but (of course) if the user''s clock is not the same as the server
then the numbers are different. D''OH!




无效问题。当服务器端数据
更可靠时,您永远不应该使用客户端数据。你有没有想过时区和类似的东西?使用

服务器时间来生成参考编号,这也使您免除了客户端JavaScript支持和不可靠技术的依赖性(例如

)在提交时更改`input''元素的值可以,但是应该在这里完成
。)

PointedEars


PS

除非确实有主题变更,否则请不要更改主题。



Null problemo. You should never use client-side data when server-side data
is more reliable. Did you think of timezones and stuff like that? Use the
server time for generating the reference number which also frees you from
dependence of client-side JavaScript support and unreliable techniques (like
changing the value of an `input'' element on submit which could, but should
not be done here.)
PointedEars

P.S.
Please don''t change the Subject unless there is really a change of subject.


这篇关于格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆