Navigation

Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

1/27/2016

Parsing string for date using Javascript


Have a CMD command that returns the user expiration date, to get it into Javascript:



var stringToParse = "You have a doctor's appointment on 2012/03/13 16:00.  Please show up on time.";
var dateString    = stringToParse.match(/\d{4}\/\d{2}\/\d{2}\s+\d{2}:\d{2}/);
var dt            = new Date(dateString);
console.log(dt);        //prints "Tue March 13 16:00:00 EDT 2012"



http://www.htmlgoodies.com/html5/javascript/date-parsing-using-javascript-and-regular-expressions.htm

7/24/2014

Format date with javascript

Looking to format date in standard   mm/dd/yyyy



 
var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();

var output = d.getFullYear() + '-' +
    ((''+month).length<2 ? '0' : '') + month + '-' +
    ((''+day).length<2 ? '0' : '') + day + ' ' +
    ((''+hour).length<2 ? '0' :'') + hour + ':' +
    ((''+minute).length<2 ? '0' :'') + minute + ':' +
    ((''+second).length<2 ? '0' :'') + second;

See this jsfiddle for a proof: http://jsfiddle.net/nCE9u/3/
You can also enclose it within function (demo is here: http://jsfiddle.net/nCE9u/4/):
 
function getISODateTime(d){
    // padding function
    var s = function(a,b){return(1e15+a+"").slice(-b)};

    // default date parameter
    if (typeof d === 'undefined'){
        d = new Date();
    };

    // return ISO datetime
    return d.getFullYear() + '-' +
        s(d.getMonth()+1,2) + '-' +
        s(d.getDate(),2) + ' ' +
        s(d.getHours(),2) + ':' +
        s(d.getMinutes(),2) + ':' +
        s(d.getSeconds(),2);
}

and use it like that:
 
getISODateTime(new Date());

or:
getISODateTime(some_other_date);

This question is a duplicate (see: How to get current date in jquery?).



http://stackoverflow.com/questions/9050763/format-date-in-jquery

3/23/2012

Javascript Message Script by Day of Week, or Date

To display message on give date/time

<script type = "text/javascript">

var GMToffset = -5;  // Your current GMT offset, whether Standard or Daylight   style="font-size: large;"   
var now = new Date();
var dy = now.getDay(); // day of week 0-6

var themonth=now.getMonth()+1;
var thetoday=now.getDate();
var thedow = now.getDay();
var hh = now.getHours();
var theminutes = now.getMinutes();

 function GetDay(intDay){
    var DayArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    return DayArray[intDay];
    }

now.setHours( now.getHours() );  // GMToffset +    + now.getTimezoneOffset() / 60

var show = "No Lab Assistant on duty<br><br> " + GetDay(thedow) + "&nbsp;" + themonth + "/" + thetoday  + "&nbsp;" + hh +":"+ theminutes ;


// The GMT offset ensures that every visitor, regardless of their timezone, will see the schedule
// that is appropriate for the site owner's local time.

// Tip - for testing purposes you can put here
//dy = 3;
//hh = 7;
// or whatever to check that the right show appears at that day/time.
// days in Javascript are 0 Sunday - 6 Saturday


if (dy == 1 ) { 
if (hh >=6 && hh <9) {show = "test<br>(856) 123-4567<br>email@cua.edu   "; }
}

if (dy == 2 ) { // 2 Tuesday
if (hh >=6 && hh <9) {show = "test<br>(856) 123-4567<br>email@cua.edu      "; }
}

if (dy == 3 || dy == 4 ) { // 2 Tuesday
if (hh >=6 && hh <9) {show = "<p>test<br>123-4567<br>email@cua.edu   </p>   "; }
}




if (dy == 6) { // Saturday Note the two = signs to mean equals
if (hh >=13 && hh <18) {show = "<p>test<br>123-4567<br>email@cua.edu   </p>    "; }
} //End of Saturday Shows

if (dy == 0) { //Sunday Shows
if (hh >=13 && hh <18) {show = "<p>test<br>123-4567<br>email@cua.edu   </p> "; }
}  // End of Sunday Shows

var a = "<em>" + show + " \u00A0 \u00A0 \u00A0 \u00A0 <em> "  ;  // spaces must be separted by Unicode spaces to prevent collapse  + hh + "&nbsp;" + dy
//document.getElementById("dj").innerHTML = a;
document.write(a);
</script>