Date.parse(2/4/2011 9:34:48 AM)

my input will be from a variable (Ticket.CreationDate) and will look like

2/4/2011 9:34:48 AM (it will vary of course)

Ideally I could pass in the variable as-is and get the different results like unknownDateFunc(Ticket.DateCreation) \ Outputs= Friday, February 4, 2011 unknownTimeFunc(Ticket.DateCreation) \ Outputs= 9:34 AM

Meddling with Date.parse() and .toDateString() and I just can't figure it out.

Resolved : using Steve Levithan Method

var datevar = dateFormat(TicketList.Tickets[Ticket].CreationDate.split(" ", 1), "fullDate"); 
// without .split(" ",1) it displayed as "Thu, Feb 24, 2011 00:00" I don't know why
var timevar = dateFormat(TicketList.Tickets[Ticket].CreationDate, "shortTime");



I would suggest a combination of 2 different Date libraries I am aware of.

The first, for parsing the date from a string, is DateJS. You can find it at Your example parses with their library fine (once you include appropriate quote marks)

// Results in Date object whose toString is:  
//    Fri Feb 04 2011 09:34:48 GMT-0600 (Central Standard Time)
Date.parse('2/4/2011 9:34:48 AM')

The other library is for creating nicely formatted string values based on your date object. This can be found at For example, from that page:

// Saturday, June 9th, 2007, 5:46:21 PM
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");

The fact that you have a static format makes a solution simple.

var dateReg = 
function parseDate(input) {
    var year, month, day, hour, minute, second,
        result = dateReg.exec(input);
    if (result) {
        year = +result[3];
        month = +result[1];
        day = +result[2];
        hour = +result[4];
        minute = +result[5];
        second = +result[6];
        if (result[7] === 'PM' && hour !== 12) {
            hour += 12;
    return new Date(year, month, day, hour, minute, second);

Create a function which takes date and in-format string and out-format string as parameter

string FormatDate(string date, string informat,string outformat)
    var culture = CultureInfo.CreateSpecificCulture("en-US");
    return DateTime.ParseExact(date, informat, culture).ToString(outformat);

FormatDate("2/4/2011 9:34:48 AM","M/d/yyyy H:m:s tt","H:m:s")               //9:34:48

You can get different format string from here


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.