Need to Get the time in correct format from JavaScript to ISODateString?

Inder Singh

Actually i am trying to convert the date from java script date to ISO date format but the time is displayed is incorrect.In this the time is 14:30 but i am getting 9:30 it should be 2:30.And i need to add 30 min to my time.This function is used to convert JavaScript date To ISO data.

function myFunction1()
{
 var val1="2013-08-08 14:30:59";
 var t = val1.split(/[- :]/);
    alert(t);
 d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
 d = new Date(d.getTime() + (30 * 60000));
 alert(ISODateString(d));
}


function ISODateString(d){
      function pad(n){return n<10 ? '0'+n : n}
      return d.getUTCFullYear()+'-'
          + pad(d.getUTCMonth()+1)+'-'
          + pad(d.getUTCDate()) +' '
          + pad(d.getUTCHours())+':'
          + pad(d.getUTCMinutes())+':'
          + pad(d.getUTCSeconds())
    }

Share with me please if you know about.I have worked jsfiddle

Praveen

There is no need for UTC and therefore remove it.

Basically what you're looking for is (converting to 12 hrs time format)

pad(d.getHours() > 12 
              ? d.getHours() - 12 : d.getHours())

Complete Code:

function ISODateString(d) {
    function pad(n) {
        return n < 10 ? '0' + n : n
    }
    return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' 
          + pad(d.getDate()) + ' ' + pad(d.getHours() > 12 
          ? d.getHours() - 12 : d.getHours()) 
          + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds())
}

Result:

input: 2013-08-08 14:30:59

output: 2013-08-08 03:00:59 //Since you increased 30 mins

JSFiddle

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Javascript to return time in format 00.00.00 from 0.0.0

分類Dev

Swift: Get correct time zone from Date Picker?

分類Dev

Date time conversion in Javascript from format "/Date(1535515200000)/"

分類Dev

Javascript regext time format detection

分類Dev

java: Calendar dont get correct date / time?

分類Dev

Current UTC time in correct format with different system settings

分類Dev

How to get current date time format in SQlite?

分類Dev

I need help to get the URL in JavaScript

分類Dev

Get correct Props value from Redux

分類Dev

Loading DataTable from DataSet with correct column widths and format

分類Dev

input string not in correct format when selecting value from dropdown

分類Dev

Invoke a Function with the Correct Parameters from an Object in JavaScript

分類Dev

need correct .htaccess configuration

分類Dev

How to read the correct time/duration values from Google Spreadsheet

分類Dev

How to get api format response from Django

分類Dev

How do I get the current date and time in the format given Below

分類Dev

Input String is not in the correct format

分類Dev

Datetime is not in correct format in MySQL

分類Dev

How to get server time using javascript

分類Dev

Could not print time after conversion from string format

分類Dev

How to change the date/time format to English from the command line?

分類Dev

How to get a correct output predictions from unet_learner (fastai)?

分類Dev

Get Hour and minute from time string

分類Dev

Javascript: Extract correct json from php script using id

分類Dev

Get UTC time and local time from NSDate object

分類Dev

Get available time ranges from an array of busy time ranges

分類Dev

Get the time difference from time varaibles stored as character

分類Dev

Javascript - displaying locale time from mySQL TimeDate

分類Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

Related 関連記事

  1. 1

    Javascript to return time in format 00.00.00 from 0.0.0

  2. 2

    Swift: Get correct time zone from Date Picker?

  3. 3

    Date time conversion in Javascript from format "/Date(1535515200000)/"

  4. 4

    Javascript regext time format detection

  5. 5

    java: Calendar dont get correct date / time?

  6. 6

    Current UTC time in correct format with different system settings

  7. 7

    How to get current date time format in SQlite?

  8. 8

    I need help to get the URL in JavaScript

  9. 9

    Get correct Props value from Redux

  10. 10

    Loading DataTable from DataSet with correct column widths and format

  11. 11

    input string not in correct format when selecting value from dropdown

  12. 12

    Invoke a Function with the Correct Parameters from an Object in JavaScript

  13. 13

    need correct .htaccess configuration

  14. 14

    How to read the correct time/duration values from Google Spreadsheet

  15. 15

    How to get api format response from Django

  16. 16

    How do I get the current date and time in the format given Below

  17. 17

    Input String is not in the correct format

  18. 18

    Datetime is not in correct format in MySQL

  19. 19

    How to get server time using javascript

  20. 20

    Could not print time after conversion from string format

  21. 21

    How to change the date/time format to English from the command line?

  22. 22

    How to get a correct output predictions from unet_learner (fastai)?

  23. 23

    Get Hour and minute from time string

  24. 24

    Javascript: Extract correct json from php script using id

  25. 25

    Get UTC time and local time from NSDate object

  26. 26

    Get available time ranges from an array of busy time ranges

  27. 27

    Get the time difference from time varaibles stored as character

  28. 28

    Javascript - displaying locale time from mySQL TimeDate

  29. 29

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

ホットタグ

アーカイブ