Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Change date format in javascript

Change date format in javascript

Scheduled Pinned Locked Moved Web Development
javascripthelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    R Thomas 0
    wrote on last edited by
    #1

    hi guys, this is one date that i have Thu Jan 10 00:00:00 UTC+0800 2002 can i change is to any other format eg. "dd-MM-yyyy hh:mm:ss" or something else using javascript on the client side? any help appreciated... thx "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

    T Richard DeemingR 2 Replies Last reply
    0
    • R R Thomas 0

      hi guys, this is one date that i have Thu Jan 10 00:00:00 UTC+0800 2002 can i change is to any other format eg. "dd-MM-yyyy hh:mm:ss" or something else using javascript on the client side? any help appreciated... thx "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

      T Offline
      T Offline
      Ted Ferenc
      wrote on last edited by
      #2

      Have you tried the Date Object?


      "An expert is a person who has made all the mistakes that can be made in a very narrow field." - Neils Bohr

      R 1 Reply Last reply
      0
      • T Ted Ferenc

        Have you tried the Date Object?


        "An expert is a person who has made all the mistakes that can be made in a very narrow field." - Neils Bohr

        R Offline
        R Offline
        R Thomas 0
        wrote on last edited by
        #3

        no.... could u help me out here..... how can i change my exsisting string -> Thu Jan 10 00:00:00 UTC+0800 2002 to a more normal string like "dd-MM-yyy" or something using the date object??? thx... "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

        T 2 Replies Last reply
        0
        • R R Thomas 0

          no.... could u help me out here..... how can i change my exsisting string -> Thu Jan 10 00:00:00 UTC+0800 2002 to a more normal string like "dd-MM-yyy" or something using the date object??? thx... "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

          T Offline
          T Offline
          Ted Ferenc
          wrote on last edited by
          #4

          strtotime possibly, seems to work with a UTC string


          "An expert is a person who has made all the mistakes that can be made in a very narrow field." - Neils Bohr

          1 Reply Last reply
          0
          • R R Thomas 0

            no.... could u help me out here..... how can i change my exsisting string -> Thu Jan 10 00:00:00 UTC+0800 2002 to a more normal string like "dd-MM-yyy" or something using the date object??? thx... "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

            T Offline
            T Offline
            Ted Ferenc
            wrote on last edited by
            #5

            Sorry I think I gave you a PHP solution, I must keep off the wine! Try this:- time = Date.parse("Mon Jan 12 10:00:00 UTC+0800 2004"); newDate = new Date(time); alert(newDate.toString());


            "An expert is a person who has made all the mistakes that can be made in a very narrow field." - Neils Bohr

            1 Reply Last reply
            0
            • R R Thomas 0

              hi guys, this is one date that i have Thu Jan 10 00:00:00 UTC+0800 2002 can i change is to any other format eg. "dd-MM-yyyy hh:mm:ss" or something else using javascript on the client side? any help appreciated... thx "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              First, you need to set up functions to return the day and month names, since the JS Date object doesn't include them:

              Date._dayNames = new Array(
              "Sunday",
              "Monday",
              "Tuesday",
              "Wednesday",
              "Thursday",
              "Friday",
              "Saturday");

              Date._dayNamesA = new Array(
              "Sun",
              "Mon",
              "Tue",
              "Wed",
              "Thu",
              "Fri",
              "Sat");

              Date.prototype.getDayName = function(abbreviate)
              {
              if (isNaN(this))
              return "";
              else if (true == abbreviate)
              return Date._dayNamesA[this.getDay()];
              else
              return Date._dayNames[this.getDay()];
              }

              Date._monthNames = new Array(
              "January",
              "February",
              "March",
              "April",
              "May",
              "June",
              "July",
              "August",
              "September",
              "October",
              "November",
              "December");

              Date._monthNamesA = new Array(
              "Jan",
              "Feb",
              "Mar",
              "Apr",
              "May",
              "June",
              "July",
              "Aug",
              "Sep",
              "Oct",
              "Nov",
              "Dec");

              Date.prototype.getMonthName = function(abbreviate)
              {
              if (isNaN(this))
              return "";
              else if (true == abbreviate)
              return Date._monthNamesA[this.getMonth()];
              else
              return Date._monthNames[this.getMonth()];
              }

              Then, you need to use a regular expression to parse the format string and substitute the real values:

              Date._reFormat = /(\\{0,1})(dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|gg|g|hh|h|ss|f+|tt|t|zzz|zz|z)/gi;

              Date.prototype.format = function(format)
              {
              if (isNaN(this)) return this.toString();
              if (!format || 0 == format.length) return this.toString();

              var value = this;
              return format.replace(Date.\_reFormat, function($0, $1, $2)
              {
                  var fmt = $2;
                  if (!fmt || 0 == fmt.length) return "";
                  
                  var ret = "";
                  if ("\\\\" == $1)
                  {
                      ret = fmt.charAt(0);
                      fmt = fmt.substr(1);
                      if (0 == fmt.length) return ret;
                  }
                  
                  switch(fmt.toLowerCase())
                  {
                      case "dddd":
                      {
                          // Full day name
                          ret += value.getDayName();
                          break;
                      }
                      case "ddd":
                      {
                          // Short day name
                          ret += value.getDayName(true);
                          break;
                      }
                      case "dd":
                      {
                          // Padded day number
                          var d = value.getDate();
              

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups