Reputation: 366
I have dataset which have different number of date time like this Mon Dec 26 2016 16:25:01 GMT+0500 (Pakistan Standard Time),
I have converted the above mention time into my dateformat in jquery but I need to convert the other column which is exactly like this but it has multiple dates in one row of html table like below
I want the last column to be exactly convert to the 1st one
Here is my JQuery code
$(document).ready(function() {
$('.dateField').each(function() {
var date = new Date($(this).text());
var dformat = [ (date.getMonth()+1),
date.getDate(),
date.getFullYear()].join('/')+
' ' +
[ date.getHours(),
date.getMinutes(),
date.getSeconds()].join(':');
$(this).text(dformat);
});
});
Upvotes: 1
Views: 2604
Reputation: 1264
UPDATE
Added the condition to check if the current string is empty. Otherwise the output will include NaN NaN. And including the jsfiddle link.
You need to split the two date(strings) and then apply your already written codes to each string, something like this:
$(document).ready(function() {
$('.dateField').each(function() {
var colVal = "";
var str = $(this).text();
var arr = str.split(",");
for(var i=0; i<arr.length; i++)
{
if(arr[i]!="")
{
var myDate = new Date(arr[i]);
var newDateStr = [ (myDate.getMonth()+1),
myDate.getDate(),
myDate.getFullYear()].join('/')+
' ' +
[ myDate.getHours(),
myDate.getMinutes(),
myDate.getSeconds()].join(':');
colVal +=newDateStr+'<br />';
}
}
});
});
jsfiddle: https://jsfiddle.net/up8qr8qv/
Upvotes: 1
Reputation: 442
Your code is basically there you only have a small amount extra to do:
(1) split up date strings which may be separated by comma(s),
(2) convert to dates (which you have done) and
(3) rejoin with breaks (as you may like...)
please see jsFiddle output below:
$(document).ready(function()
{
$('.dateField').each(function()
{
var sDate = new $(this).text(); // string date(s) (may be separated by comma(s))
var aDate = sDate.split(','); // array of dates
for(var i = 0, length = aDate.length; i < length; i++)
{
// Convert date format
var date = new Date(aDate[i]);
var dformat =
[(date.getMonth() + 1),
date.getDate(),
date.getFullYear()
].join('/') + ' ' +
[ date.getHours(),
date.getMinutes(),
date.getSeconds()
].join(':');
aDate[i] = dformat;
}
$(this).html(aDate.join('<br>')); // join dates with breaks in between
});
});
td {
border: 1px solid black;
vertical-align:top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="dateField">
Mon Dec 26 2016 16:25:01 GMT+0500 (Pakistan Standard Time)
</td>
<td class="dateField">
Mon Dec 26 2016 16:25:01 GMT+0500 (Pakistan Standard Time),Mon Dec 26 2016 16:25:01 GMT+0500 (Pakistan Standard Time)
</td?
</tr>
</table>
You may want to change the converter to make months, days, hours, minutes & seconds < 9 to be zero padded in front ie 9 -> 09
function ZeroPad(val)
{
if(val * 1 < 10)
{
return '0' + val;
}
return val;
}
$(document).ready(function()
{
$('.dateField').each(function()
{
var sDate = new $(this).text(); // string date(s) (may be separated by comma(s))
var aDate = sDate.split(','); // array of dates
for(var i = 0, length = aDate.length; i < length; i++)
{
// Convert date format
var date = new Date(aDate[i]);
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var dformat =
[ ZeroPad(month),
ZeroPad(day),
date.getFullYear()
].join('/') + ' ' +
[ ZeroPad(hour),
ZeroPad(min),
ZeroPad(sec)
].join(':');
aDate[i] = dformat;
}
$(this).html(aDate.join('<br><br>')); // join dates with breaks in between
});
});
td {
border: 1px solid black;
vertical-align:top;
width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="dateField">
Wed Jan 6 2016 06:05:01 GMT+0500 (Pakistan Standard Time)
</td>
<td class="dateField">
Mon Dec 26 2016 16:25:01 GMT+0500 (Pakistan Standard Time),Tue Dec 27 2016 04:02:03 GMT+0500 (Pakistan Standard Time)
</td?
</tr>
</table>
Upvotes: 1