Javascript set time string to date object

Asked
Active3 hr before
Viewed126 times

8 Answers

objectjavascript
90%

GitLab launches Collective on Stack Overflow , Meta Stack Overflow ,Thanks for contributing an answer to Stack Overflow!, Stack Overflow Public questions & answers

var d = new Date(),
   s = "01.25 PM",
   parts = s.match(/(\d+)\.(\d+) (\w+)/),
   hours = /am/i.test(parts[3]) ? parseInt(parts[1], 10) : parseInt(parts[1], 10) + 12,
   minutes = parseInt(parts[2], 10);

d.setHours(hours);
d.setMinutes(minutes);

alert(d);
var test, parts, hours, minutes, date,
   d = (new Date()).getTime(),
   tests = ['01.25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
   i = tests.length,
   timeReg = /(\d+)\.(\d+) (\w+)/;

for (; i-- > 0;) {
   test = tests[i];

   parts = test.match(timeReg);

   hours = /am/i.test(parts[3]) ?
      function(am) {
         return am < 12 ? am : 0
      }(parseInt(parts[1], 10)) :
      function(pm) {
         return pm < 12 ? pm + 12 : 12
      }(parseInt(parts[1], 10));

   minutes = parseInt(parts[2], 10);

   date = new Date(d);

   date.setHours(hours);
   date.setMinutes(minutes);

   console.log(test + ' => ' + date);
}
load more v
88%

If IPOdate is an existing Date object, it can be set to August 9, 1995 (local time) as follows: , The standard string representation of a date time string is a simplification of the ISO 8601 calendar date extended format. (See the section Date Time String Format in the ECMAScript specification for more details.) , A string representing a simplification of the ISO 8601 calendar date extended format. (Other formats may be used, but results are implementation-dependent.) , Note: This section contains implementation-specific behavior that can be inconsistent across implementations.

Date.parse(dateString)
load more v
72%

var myDate = new Date("2013/1/16");

var str = "2013/1/16";
var strToDate = new Date(str);
load more v
65%

Setting date components,Access date components,Write a function formatDate(date) that should format date as follows:,The following methods allow to set date/time components:

let now = new Date();
alert(now); // shows current date/time
load more v
75%

The Date object is used to work with dates and times.,If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:,Get certifiedby completinga course today!,There are four ways of instantiating a date:

The Date object is used to work with dates and times.

Date objects are created with new Date().

new Date()
40%

JavaScript - Date , JavaScript - Date Methods , What is JavaScript , Use the Date() function to get the string representation of the current date and time in JavaScript. Use the new keyword in JavaScript to get the Date object.

Date(); //Returns current date and time string

//or

var currentDate = new Date(); //returns date object of current date and time
load more v
22%

Converting a string to a JavaScript date object is done in different ways.,Convert both dates to UTC time stamp, find the difference in milliseconds and find the equivalent days.,Here’s an example of adding 20 days to a JavaScript Date (i.e., figuring out the date 20 days after a known date):,If you are sure of the format you want to use, it is best to extract individual bits using the JavaScript functions we covered above and create a string yourself.

If you are sure of the format you want to use, it is best to extract individual bits using the JavaScript functions we covered above and create a string yourself.

var currentDate = new Date();
var date = currentDate.getDate();
var month = currentDate.getMonth();
var year = currentDate.getFullYear();

We can get the date in MM/DD/YYYY format as

var monthDateYear = (month + 1) + "/" + date + "/" + year;

We can address this by using a “pad” function that adds a leading 0.

function pad(n) {
   return n < 10 ? '0' + n : n;
}

Now, we get the correct date in MM/DD/YYYY format using:

var mmddyyyy = pad(month + 1) + "/" + pad(date) + "/" + year;

If we want DD-MM-YYYY instead, the process is similar:

var ddmmyyyy = pad(date) + "-" + pad(month + 1) + "-" + year;

Let’s up the ante and try to print the date in “Month Date, Year” format. We will need a mapping of month indexes to names:

var monthNames = [
   "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
];

var dateWithFullMonthName = monthNames[month] + " " + pad(date) + ", " + year;

Some people like to display the date as 1st January, 2013. No problem, all we need is a helper function ordinal that returns 1st for 1, 12th for 12, and 103rd for 103, etc., and the rest is simple:

var ordinalDate = ordinal(date) + " " + monthNames[month] + ", " + year;

It is easy to determine the day of week from the date object, so let’s add that in:

var daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

ordinalDateWithDayOfWeek = daysOfWeek[currentDate.getDay()] + ", " + ordinalDate;
load more v
60%

To achieve all of these objectives and more, JavaScript comes with the built in Date object and related methods. This tutorial will go over how to format and use date and time in JavaScript., Understanding Date and Time in JavaScript , The Date Object , Understanding This, Bind, Call, and Apply in JavaScript

// Set variable to current date and time
const now = new Date();

// View the output
now;
load more v

Other "object-javascript" queries related to "Javascript set time string to date object"