8

Hi I was wondering if there is any jquery function around which can take this dateTime "2010-10-18 10:06" and convert and split it returning "2010/10/18" and "10:06".

It would be also nice if the same function could either receive "2010-10-18 10:06" or "2010-10-18" only and return as mentioned above, or different formats besides "2010/10/18" like 18-10-2010" or and 18th of October 2010, giving the option but not that important, just curious about jQuery power dealing with dates.

Thanks.

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
Amra
  • 24,780
  • 27
  • 82
  • 92
  • I wouldn't say that the first part of this question is particularly a JQuery issue - just general Javascript. Are you input values strings or date objects? – belugabob Mar 08 '10 at 15:41
  • They come from database, but I thinks they are treated as string. – Amra Mar 08 '10 at 15:51
  • So my example will do the trick just nicely! – Marcos Placona Mar 08 '10 at 15:55
  • Well then, there are two question in one here. The first question asks how to split a datetime, expressed as a string, into the date and time parts. This can easily be acheived using the javascript string 'split' function, using a space as the separator. This results in a two element array, with the first element containg the date and the second containing the time. The second part of the question asks how to 'do the same' with a date time, or just a date - I'm not sure what the OP would expect the time part to contain is the case of just a date being supplied? – belugabob Mar 09 '10 at 10:00

5 Answers5

19

Converting with DateJs should be as easy as:

var d1 = Date.parse('2010-10-18, 10:06 AM');
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT'));

It's currently the best library around

kero
  • 10,647
  • 5
  • 41
  • 51
Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
  • it says invalid procedure :-(, i copied your example – Amra Mar 09 '10 at 10:02
  • Sorry I did not downloaded the datejs. – Amra Mar 09 '10 at 10:21
  • For one small operation, using entire file is not good I think.. Look at below answer by Henry, we can split date and time in to two parts... correct me if I am in wrong direction.. – gkd Apr 02 '14 at 18:58
16

Without a any external jQuery plugin like DateJs. We can get the date as given below.

var datetime= '2010-10-18 10:06 AM' // Default datetime will be like this.

//By Spliting the input control value with space
var date=datetime.split(' ')[0];
//date -2010-10-18
Henry
  • 311
  • 2
  • 6
  • 1
    Really nice idea Henry, sometime we unknowingly do 1+1 in "1 - 1 + 1 + 1" way.. – gkd Apr 02 '14 at 18:55
  • 1
    Yes this is definitely appropriate for this. Of course use [1] to get the time instead of date :) – klidifia Sep 08 '14 at 21:54
6
<input type="text" id="tbDateTime" value="2010-10-18 10:06" />
<input type="text" id="tbDate" value="" />
<input type="text" id="tbTime" value="" />

<input type="button" id="btnSubmit" value="Submit" />


<script type="text/javascript">
    $(function () {
        $('#btnSubmit').click(function () {
            var dateTimeSplit = $('#tbDateTime').val().split(' ');

            var dateSplit = dateTimeSplit[0].split('-');
            var currentDate = dateSplit[2] + '/' + dateSplit[1] + '/' + dateSplit[0];
            //currentDate is 18/10/2010

            $('#tbDate').val(currentDate);

            var currentTime = dateTimeSplit[1];
            //currentTime is 10:06

            $('#tbTime').val(currentTime);
        });
    });
</script>
Jehof
  • 34,674
  • 10
  • 123
  • 155
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
2

datejs. Check it, its cool and it does pretty good job for all the possibilities and error handling is also pretty good.

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
0

Have a look Here

It includes a function called fromString which would help you.

j0k
  • 22,600
  • 28
  • 79
  • 90
Josh
  • 6,256
  • 2
  • 37
  • 56