Solved

convert 2014-02-04 00:00:00 to dd/mm/yyyy in jquery

Posted on 2014-07-29
6
1,353 Views
Last Modified: 2014-08-01
Hi All,
    I'm currently retrieve the date field from Sharepoint and display on a page using jquery, the date retrieved using SPServices as below:

2014-02-04 00:00:00

how to convert the date to dd/mm/yyyy format?

My code:
$().SPServices({
                      operation: "GetListItems",
                      async: false,
                      listName: "LibraryName",
                      CAMLViewFields: "<ViewFields><FieldRef Name='DateToDisplay'/></ViewFields>",
                      CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + ID + "</Value></Eq></Where></Query>",
                      completefunc: function (xData, Status) {
                        $(xData.responseXML).SPFilterNode("z:row").each(function() {
                          DateToDisplay= ($(this).attr("ows_DateToDisplay"));
                        });
                }
                  });
0
Comment
Question by:seebeelim
6 Comments
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 40227016
This extra date formatting plugin comes highly recommended;

https://github.com/phstc/jquery-dateFormat

The documentation shows how to apply a mask to the conversion.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40227211
date="2014-02-04 00:00:00"
date=date.replace(/-/g,"/").split(" ")

date=new Date(date[0])
alert(addzero(date.getDate()) + '/' + addzero(date.getMonth() + 1) + '/' +  date.getFullYear());

function addzero(el) {
    return ("0" + el).slice(-2)
}

Open in new window

0
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40227324
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 15

Accepted Solution

by:
Insoftservice earned 215 total points
ID: 40232505
You can use date.js to achieve this:

var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');

Alternatively, you can do it natively like this:

var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[0] + '-' + dateAr[1] + '-' + dateAr[2];
0
 

Author Comment

by:seebeelim
ID: 40233957
Hi insoftservice,
   What if i want to convert "2014-02-04 00:00:00" together with the time not just date?
0
 

Author Comment

by:seebeelim
ID: 40233985
Hi insoftservice. Thanks for the code. I just updated with extra yy and it works.

var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yyyy');
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now