Solved

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

Posted on 2014-07-29
6
1,502 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

730 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