Solved

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

Posted on 2014-07-29
6
1,403 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

863 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

23 Experts available now in Live!

Get 1:1 Help Now