/    Sign up×
Community /Pin to ProfileBookmark

Date format convert for entire section of date fields in the DOM

Want to apply date format to convert dd-mm-yyyy to dd/mm/yyyy to the entire form especially the result table in the DOM below.

“`
<tbody id=”gradname”>

<script>
graduandHasRows = true;
</script>
<tr id=”1″ data-gradname-rec=”” style=”background-color: rgb(242, 242, 242);”>
<td style=”padding:3px 5px;”>
1
</td>
<td style=”padding:3px 5px;” class=”dateformat” data-create-dt=””>
<a href=”#” onclick=”show_detail(this); return false;”>2020-03-20 10:48:29.0</a>
</td>
<td style=”padding:3px 5px;” data-gradname=””>
Brook
</td>
<td style=”padding:3px 5px;” class=”dateformat” data-valid-from=””>
2020-03-17 00:00:00.0
</td>
<td style=”padding:3px 5px;” class=”dateformat” data-valid-to=””>
2020-03-20 00:00:00.0
</td>
<td style=”padding:3px 5px;” class=”dateformat”>
2020-03-20 10:48:29.0
</td>
<td style=”padding:3px 5px;”>
SIS
</td>
<td style=”padding:3px 5px;” data-person-id=””>
231015769
</td>
</tr>

</tbody>
“`

Below are the jquery I tried so

` $(‘.dateformat’).text() ` Gives all dates in the form as long string returns

” 2020-03-20 10:48:29.0 2020-03-17 00:00:00.0 2020-03-20 00:00:00.0 2020-03-20 10:48:29.0

` formatDate($(‘.dateformat’).text());` function returns right format
“20/03/2020”
The function is as below

“`
function formatDate (input) {
var datePart = input.match(/d+/g),
year = datePart[0],
month = datePart[1], day = datePart[2];

return day+’/’+month+’/’+year;
}
“`

Please advise how to using Jquery convert the date format of all dates shown on the page.

to post a comment
HTMLJavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 20.2020 — You need to use jQuery's function each:
$('.dateformat').each(function(idx, item) {
item.text(formatDate(item.text());
});
Copy linkTweet thisAlerts:
@techtalkauthorMar 23.2020 — @Sempervivum#1616435

Getting this error

item.text is not a function
Copy linkTweet thisAlerts:
@SempervivumMar 23.2020 — Sorry, I was not aware that `item` is not a jQuery object but a plain DOM element. This works:
$('.dateformat').each(function (idx, item) {
$(item).text(formatDate($(item).text()));
});
×

Success!

Help @techtalk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...