将 Javascript 字符串格式化为 03 而不是 3?

发布于 2024-10-21 13:28:03 字数 1076 浏览 3 评论 0原文

我有一个 Javascript,可以以 html 格式打开今天的文件。

function openToday()
{   
    var today = new Date();
    var strYear = today.getFullYear();
    var strMonth = today.getMonth();
    var strDay = today.getDate();

    var strURL = "file:/time/"+strYear+"/"+strMonth+"/" + strYear+"_"+strMonth+"_"+strDay+ "/" + strYear+"_"+strMonth+"_"+strDay+".html";

    alert(strURL);
    window.open(strURL,"myWindow");
}

问题是我想要 2011_03_10,但代码给了我 2011_3_10。 如何将 Javascript 字符串格式化为 03 而不是 3?

编辑

此代码工作正常

function openToday()
{   
    var today = new Date();
    var strYear = today.getFullYear();
    var strMonth = today.getMonth();
    strMonth += 1;
    if(strMonth < 10){
        strMonth = "0" + strMonth;
    }
    var strDay = today.getDate();
    if(strDay < 10){
        strDay = "0" + strDay;
    }

    var strURL = "file:/time/"+strYear+"/"+strMonth+"/" + strYear+"_"+strMonth+"_"+strDay+ "/" + strYear+"_"+strMonth+"_"+strDay+".html";

    window.open(strURL,"myWindow");
}

I have a Javascript that opens today file in html.

function openToday()
{   
    var today = new Date();
    var strYear = today.getFullYear();
    var strMonth = today.getMonth();
    var strDay = today.getDate();

    var strURL = "file:/time/"+strYear+"/"+strMonth+"/" + strYear+"_"+strMonth+"_"+strDay+ "/" + strYear+"_"+strMonth+"_"+strDay+".html";

    alert(strURL);
    window.open(strURL,"myWindow");
}

The problem is that I want to have the 2011_03_10, but the code gives me 2011_3_10.
How can I format the Javascript string to have 03 not 3?

EDIT

This code works fine

function openToday()
{   
    var today = new Date();
    var strYear = today.getFullYear();
    var strMonth = today.getMonth();
    strMonth += 1;
    if(strMonth < 10){
        strMonth = "0" + strMonth;
    }
    var strDay = today.getDate();
    if(strDay < 10){
        strDay = "0" + strDay;
    }

    var strURL = "file:/time/"+strYear+"/"+strMonth+"/" + strYear+"_"+strMonth+"_"+strDay+ "/" + strYear+"_"+strMonth+"_"+strDay+".html";

    window.open(strURL,"myWindow");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

毁梦 2024-10-28 13:28:03

检查月份是否只有 1 个字符长(或者 < 9)。然后在前面加上0!

按长度

var strMonth = today.getMonth();

if(strMonth .length == 1){
    strMonth = "0" + strMonth ;
}

按数字

var strMonth = today.getMonth();

if(strMonth< 10){
    strMonth= "0" + strMonth;
}

可能希望避免在变量前加上 str 前缀,因为 Javascript 没有明确定义类型,并且可能会混淆代码。例如,如果 strMonth 10 逻辑上很好,但维护方面管理起来很混乱。

另一种方式!

var strMonth = "0" + today.getMonth();
strMonth = strMonth.substring(strMonth.length-2, 2);

Check to see if the month is only 1 character long (or alternatively, < 9). Then prepend the 0!

By length

var strMonth = today.getMonth();

if(strMonth .length == 1){
    strMonth = "0" + strMonth ;
}

By number

var strMonth = today.getMonth();

if(strMonth< 10){
    strMonth= "0" + strMonth;
}

Probably want to avoid prefixing the variable with str as Javascript doesn't explicitly define types, and can confuse the code. For example, saying if strMonth < 10 is fine logic wise, but maintainance wise it's confusing to manage.

Another Way!

var strMonth = "0" + today.getMonth();
strMonth = strMonth.substring(strMonth.length-2, 2);
新一帅帅 2024-10-28 13:28:03

您可以创建一个通用的填充函数

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

pad(today.getDay(), 2); // If today was '3', would print '03'

You could create a general-purpose padding function:

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

pad(today.getDay(), 2); // If today was '3', would print '03'
谎言 2024-10-28 13:28:03

我前段时间为此做了一个功能。

var strURL = "file:/time/"+strYear+"/"+convertDateToString(date.getMonth()+1)+"/" +   strYear+"_"+convertDateToString(date.getMonth()+1)+"_"+strDay+ "/" +   strYear+"_"+strMonth+"_"+strDay+".html";

功能:

/*
Method: convertDateToString
Input: Integer  
Returns: a string from a number and adds a 0 when the number is smaller than 10

Examples: 1 => 01, 8 => 08, 11 => 11
*/ 
function convertDateToString(number){   
  return (number < 10 ) ? 0+number.toString() : number.toString();
}

祝你好运!

I made a function for that some time ago.

var strURL = "file:/time/"+strYear+"/"+convertDateToString(date.getMonth()+1)+"/" +   strYear+"_"+convertDateToString(date.getMonth()+1)+"_"+strDay+ "/" +   strYear+"_"+strMonth+"_"+strDay+".html";

The function:

/*
Method: convertDateToString
Input: Integer  
Returns: a string from a number and adds a 0 when the number is smaller than 10

Examples: 1 => 01, 8 => 08, 11 => 11
*/ 
function convertDateToString(number){   
  return (number < 10 ) ? 0+number.toString() : number.toString();
}

Good luck!

随遇而安 2024-10-28 13:28:03
var strMonth = today.getMonth();
if(strMonth.length == 1){
   strMonth = '0' + strMonth;
}
var strMonth = today.getMonth();
if(strMonth.length == 1){
   strMonth = '0' + strMonth;
}
人心善变 2024-10-28 13:28:03

也许您可以扩展它以允许填充字符串,如下所示:

function pad(number, length, padWith) {
    padWith = (typeof padWith!=='undefined) ? padWith : '0';
    var str = '' + number;
    while (str.length < length) {
        str = padWith + str;
    }
    return str;
}

Perhaps you may extend it to allow for padding strings like this:

function pad(number, length, padWith) {
    padWith = (typeof padWith!=='undefined) ? padWith : '0';
    var str = '' + number;
    while (str.length < length) {
        str = padWith + str;
    }
    return str;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文