Solved

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

Posted on 2014-07-29
6
1,536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 32

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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)

688 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