Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Thursday, January 28, 2010

Replace Newline to br tag

function nl2br_js(myString) {
var regX = /\n/gi ;

s = new String(myString);
s = s.replace(regX, "
\n");
return s;
}

Tuesday, April 7, 2009

Validate Date in Javascript

Format -: mm/dd/yyyy

function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correctand submit again.")
else{
var monthfield=input.value.split("/")[0]
var dayfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||dayobj.getDate()!=dayfield)||dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month,or Year rangedetected. Please correctand submit again.")
else
returnval=true
}
if(returnval==false)
input.select()
return returnval
}

Friday, March 13, 2009

Javascript-Working with dates

JavaScript Date Object Methods to get date parts:

getDate()
getDate
function returns the current date from 1 to 31 according to the corresponding month.

getDay()
getDay
function returns the current Day of the week from 0 to 6 where 0 for Sunday, 1 for Monday and so on.

getMonth()
getMonth
function returns the current month from 0 to 11 where 0 for January, 1 for February and so on.

getFullYear()
getFullYear
function returns the current year e.g. 2008


Methods to retrieve date values

The following methods all extract information from the Date object:

Method Description Example
getFullYear () Retrieves the year component of the date as a 4-digit number. y = x.getFullYear ()



getMonth() Retrieves the month component of the date as a number from 0 to 11 (0=January, 1=February, etc) y = x.getMonth()



getDate() Retrieves the day-of-month component of the date as a number from 1 to 31 y = x.getDate()



getDay() Retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc) y = x.getDay()



getHours() Retrieves the hours component of the date as a number from 0 to 23 y = x.getHours()



getMinutes() Retrieves the minutes component of the date as a number from 0 to 59 y = x.getMinutes()



getSeconds() Retrieves the seconds component of the date as a number from 0 to 59 y = x.getSeconds()



getMilliseconds() Retrieves the milliseconds component of the date as a number from 0 to 999 y = x.getMilliseconds()



getTimezoneOffset() Retrieves the time difference, in minutes, between the computer's local time and GMT y = x.getTimezoneOffset()

The following methods are similar to the previous ones, but they work with UTC (Universal Time Coordinated) time, or GMT, rather than the computer's local time.

About UTC

UTC is a timezone-independent way of storing time values, based on milliseconds since midnight, January 1, 1970 in the GMT timezone. All dates and times are stored internally in JavaScript using UTC. When you use the above, non-UTC functions to get dates and times, JavaScript converts the time back into the local computer's time locale first. However, the UTC functions below will return times and dates in UTC format instead.

Method Description Example
getUTCFullYear () Retrieves the year component of the date as a 4-digit number. y = x.getUTCFullYear ()



getUTCMonth() Retrieves the month component of the date as a number from 0 to 11 (0=January, 1=February, etc) y = x.getUTCMonth()



getUTCDate() Retrieves the day-of-month component of the date as a number from 1 to 31 y = x.getUTCDate()



getUTCDay() Retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc) y = x.getUTCDay()



getUTCHours() Retrieves the hours component of the date as a number from 0 to 23 y = x.getUTCHours()



getUTCMinutes() Retrieves the minutes component of the date as a number from 0 to 59 y = x.getUTCMinutes()



getUTCSeconds() Retrieves the seconds component of the date as a number from 0 to 59 y = x.getUTCSeconds()



getUTCMilliseconds() Retrieves the milliseconds component of the date as a number from 0 to 999 y = x.getUTCMilliseconds()



Methods to set date values

The following methods allow you to set components of the Date object:

Method Description Example
setFullYear() Sets the year component of the date using a 4-digit number. x.setFullYear(2002)



setMonth() Sets the month component of the date using a number from 0 to 11 (0=January, 1=February, etc) x.setMonth(11)



setDate() Sets the day-of-month component of the date using a number from 1 to 31 x.setDate(17)



setMinutes() Sets the minutes component of the date using a number from 0 to 59 x.setMinutes(45)



setSeconds() Sets the seconds component of the date using a number from 0 to 59 x.setSeconds(0)



setMilliseconds() Sets the milliseconds component of the date using a number from 0 to 999 x.setMilliseconds(500)

And again, these methods set date components using UTC, rather than the computer's local time:

Method Description Example
setUTCFullYear() Sets the year component of the date using a 4-digit number. x.setUTCFullYear(2002)



setUTCMonth() Sets the month component of the date using a number from 0 to 11 (0=January, 1=February, etc) x.setUTCMonth(11)



setUTCDate() Sets the day-of-month component of the date using a number from 1 to 31 x.setUTCDate(17)



setUTCHours() Sets the hours component of the date using a number from 0 to 23 x.setUTCHours(20)



setUTCMinutes() Sets the minutes component of the date using a number from 0 to 59 x.setUTCMinutes(45)



setUTCSeconds() Sets the seconds component of the date using a number from 0 to 59 x.setUTCSeconds(0)



setUTCMilliseconds() Sets the milliseconds component of the date using a number from 0 to 999 x.setUTCMilliseconds(500)



Other Date methods

There is a handful of further methods that can be used on Date objects:

Method Description Example
parse() Converts a date text string to the "milliseconds since midnight Jan 1, 1970" format. You call it using Date.parse() rather than object.parse(). x = Date.parse
("January 6, 1972")



UTC(y, m, d, h, m, s) Converts the specified date to the "milliseconds since midnight Jan 1, 1970" format. You call it using Date.UTC() rather than object.UTC(). x = Date.UTC(1972, 0, 6)

x = Date.UTC
(1972, 0, 6, 16, 5, 0)



toString() Outputs the value of the Date object as a date text string. y = x.toString()



toUTCString() Outputs the value of the Date object as a date text string, using UTC. y = x.toUTCString()



toLocaleString() Outputs the value of the Date object as a date text string, using the computer's locale conventions (e.g. D/M/Y for UK computers). y = x.toLocaleString()



toDateString() Outputs the "date" portion of the Date object as a text string. y = x.toDateString()



toTimeString() Outputs the "time" portion of the Date object as a text string. y = x.toTimeString()