如何获得某些特定格式的时间?

时间:2016-10-26 10:49:05

标签: javascript

我应该如何获得某些特定格式的时间,比如我想得到时间hh:mm am / pm然后添加一些分钟。 例如,我的日期2016-09-16T11:40:50.000Z首先得到时间11:40 am,然后再添加20分钟,得到12:00 am之类的结果 那么,我该怎么做呢。 我尝试date.getTime(),但它没有给我我想要的适当时间。

4 个答案:

答案 0 :(得分:2)

function gettime(date) {
 var hours = date.getHours();
 var minutes = date.getMinutes();
 var ampm = hours >= 12 ? 'pm' : 'am';
 hours = hours % 12;
 hours = hours ? hours : 12; // the hour '0' should be '12'
 minutes = minutes < 10 ? '0'+minutes : minutes;
 return = hours + ':' + minutes + ' ' + ampm;

}

答案 1 :(得分:1)

for adding more minutes you can use 
   date.getMinutes() + 20; //It will add 20 minutes

答案 2 :(得分:0)

如果您正在谈论'These Options declaration always go at the top of the module, class, etc. Option Explicit Option Base 1 Sub Get_Overlap() Const kFlag As String = "Overlapping" 'Change as required Dim rData As Range, aData As Variant, aResults() As String, sResult As String Dim lA As Long, sAvalue As String, iAp As Integer, bA1 As Byte, bA2 As Byte Dim lB As Long, sBvalue As String, iBp As Integer, bB1 As Byte, bB2 As Byte Rem Sets Data Range & Arrays With ThisWorkbook.Sheets("TEST").Columns("B") 'Change as required Set rData = Range(.Cells(2), .Cells(Rows.Count).End(xlUp)) End With aData = rData.Value2 aData = WorksheetFunction.Transpose(aData) rData.Offset(0, 1).ClearContents ReDim Preserve aResults(UBound(aData)) For lA = 1 To UBound(aData) Rem Initialize & Set Item A Values sAvalue = Empty: sAvalue = aData(lA) iAp = 0: iAp = Left(sAvalue, 1) bA1 = 0: bA1 = Mid(sAvalue, 3, 1) On Error Resume Next bA2 = 0: bA2 = Mid(sAvalue, 4, 1) On Error GoTo 0 For lB = lA + 1 To UBound(aData) Rem Initialize & Set Item B Values sBvalue = Empty: sBvalue = aData(lB) iBp = 0: iBp = Left(sBvalue, 1) bB1 = 0: bB1 = Mid(sBvalue, 3, 1) On Error Resume Next bB2 = 0: bB2 = Mid(sBvalue, 4, 1) On Error GoTo 0 Rem Initialize Comparison Result sResult = Empty Rem Compare Items & Values Select Case True Case sAvalue = sBvalue sResult = kFlag Case iAp = iBp Select Case True Case bA2 = 0 And bB2 = 0 If (bA1 = bB1) Then sResult = kFlag Case bA2 = 0 If bA1 >= bB1 And bA1 <= bB2 Then sResult = kFlag Case bB2 = 0 If bB1 >= bA1 And bB1 <= bA2 Then sResult = kFlag Case Else If bA1 >= bB1 And bA1 <= bB2 Then sResult = kFlag ElseIf bA2 >= bB1 And bA2 <= bB2 Then sResult = kFlag ElseIf bB1 >= bA1 And bB1 <= bA2 Then sResult = kFlag ElseIf bB2 >= bA1 And bB2 <= bA2 Then sResult = kFlag End If End Select: End Select Rem Add Results into Array If sResult <> Empty Then aResults(lA) = sResult aResults(lB) = sResult End If Next: Next Rem Enter Comparison Results 'Results will be posted one column to the right of where the List 'This is done by the use of "rData.Offset(0,1)" rData.Offset(0, 1).Value = WorksheetFunction.Transpose(aResults) End Sub 函数,那么使用此函数没有直接的方法来形成字符串,因为它总是返回表示秒的整数。

答案 3 :(得分:-1)

function formatDate(date, format, utc) {
    var MMMM = ["\x00", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var MMM = ["\x01", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var dddd = ["\x02", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var ddd = ["\x03", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

    function ii(i, len) {
        var s = i + "";
        len = len || 2;
        while (s.length < len) s = "0" + s;
        return s;
    }

    var y = utc ? date.getUTCFullYear() : date.getFullYear();
    format = format.replace(/(^|[^\\])yyyy+/g, "$1" + y);
    format = format.replace(/(^|[^\\])yy/g, "$1" + y.toString().substr(2, 2));
    format = format.replace(/(^|[^\\])y/g, "$1" + y);

    var M = (utc ? date.getUTCMonth() : date.getMonth()) + 1;
    format = format.replace(/(^|[^\\])MMMM+/g, "$1" + MMMM[0]);
    format = format.replace(/(^|[^\\])MMM/g, "$1" + MMM[0]);
    format = format.replace(/(^|[^\\])MM/g, "$1" + ii(M));
    format = format.replace(/(^|[^\\])M/g, "$1" + M);

    var d = utc ? date.getUTCDate() : date.getDate();
    format = format.replace(/(^|[^\\])dddd+/g, "$1" + dddd[0]);
    format = format.replace(/(^|[^\\])ddd/g, "$1" + ddd[0]);
    format = format.replace(/(^|[^\\])dd/g, "$1" + ii(d));
    format = format.replace(/(^|[^\\])d/g, "$1" + d);

    var H = utc ? date.getUTCHours() : date.getHours();
    format = format.replace(/(^|[^\\])HH+/g, "$1" + ii(H));
    format = format.replace(/(^|[^\\])H/g, "$1" + H);

    var h = H > 12 ? H - 12 : H == 0 ? 12 : H;
    format = format.replace(/(^|[^\\])hh+/g, "$1" + ii(h));
    format = format.replace(/(^|[^\\])h/g, "$1" + h);

    var m = utc ? date.getUTCMinutes() : date.getMinutes();
    format = format.replace(/(^|[^\\])mm+/g, "$1" + ii(m));
    format = format.replace(/(^|[^\\])m/g, "$1" + m);

    var s = utc ? date.getUTCSeconds() : date.getSeconds();
    format = format.replace(/(^|[^\\])ss+/g, "$1" + ii(s));
    format = format.replace(/(^|[^\\])s/g, "$1" + s);

    var f = utc ? date.getUTCMilliseconds() : date.getMilliseconds();
    format = format.replace(/(^|[^\\])fff+/g, "$1" + ii(f, 3));
    f = Math.round(f / 10);
    format = format.replace(/(^|[^\\])ff/g, "$1" + ii(f));
    f = Math.round(f / 10);
    format = format.replace(/(^|[^\\])f/g, "$1" + f);

    var T = H < 12 ? "AM" : "PM";
    format = format.replace(/(^|[^\\])TT+/g, "$1" + T);
    format = format.replace(/(^|[^\\])T/g, "$1" + T.charAt(0));

    var t = T.toLowerCase();
    format = format.replace(/(^|[^\\])tt+/g, "$1" + t);
    format = format.replace(/(^|[^\\])t/g, "$1" + t.charAt(0));

    var tz = -date.getTimezoneOffset();
    var K = utc || !tz ? "Z" : tz > 0 ? "+" : "-";
    if (!utc) {
        tz = Math.abs(tz);
        var tzHrs = Math.floor(tz / 60);
        var tzMin = tz % 60;
        K += ii(tzHrs) + ":" + ii(tzMin);
    }
    format = format.replace(/(^|[^\\])K/g, "$1" + K);

    var day = (utc ? date.getUTCDay() : date.getDay()) + 1;
    format = format.replace(new RegExp(dddd[0], "g"), dddd[day]);
    format = format.replace(new RegExp(ddd[0], "g"), ddd[day]);

    format = format.replace(new RegExp(MMMM[0], "g"), MMMM[M]);
    format = format.replace(new RegExp(MMM[0], "g"), MMM[M]);

    format = format.replace(/\\(.)/g, "$1");

    return format;
};