function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SunilSunil 

How to store date itto Date field?

How to store date into Date field using scontrol? I am using follwing code to store date into custom field: -
 
tru.set("TB1__c","6/8/2007");
I am getting error "6/8/2007' is not a valid for the type 'xsd:date'.
 
Please advise, how can store date. This is really urgent.
 
Thanks.
 
 
 
Pat McQueenPat McQueen

Try this ...

http://wiki.apexdevnet.com/index.php/Create_a_many_to_many_relationship_with_a_multi-select_picklist

function toIsoDateTime(theDate) {

var today = new Date(theDate);
var year = today.getYear();
if (year < 2000) {
year = year + 1900;
}
var month = today.getMonth() + 1;
var day = today.getDate();
var hour = today.getHours();
var hourUTC = today.getUTCHours();
var diff = hour - hourUTC;
var hourdifference = Math.abs(diff);
var minute = today.getMinutes();
var minuteUTC = today.getUTCMinutes();
var minutedifference;
var second = today.getSeconds();
var timezone;

if (minute != minuteUTC && minuteUTC < 30 && diff < 0) { hourdifference--; }
if (minute != minuteUTC && minuteUTC > 30 && diff > 0) { hourdifference--; }
if (minute != minuteUTC) {
minutedifference = ":30";
} else {
minutedifference = ":00";
}
if (hourdifference < 10) {
timezone = "0" + hourdifference + minutedifference;
} else {
timezone = "" + hourdifference + minutedifference;
}
if (diff < 0) {
timezone = "-" + timezone;
} else {
timezone = "+" + timezone;
}
if (month <= 9) month = "0" + month;
if (day <= 9) day = "0" + day;
if (hour <= 9) hour = "0" + hour;
if (minute <= 9) minute = "0" + minute;
if (second <= 9) second = "0" + second;
return year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + timezone;
}

mikefmikef
Sunil:

If all you want to do is change the date a user enters in a web form to a date the api likes you can use this silly little function I wrote.

Pass in the date value and the type which is 'api' or 'user', type 'api' returns the api version of the date.

This is not pretty or elegant in any way but it gets the job done. Keep in mind it only works for US formated dates.

Code:
function apiDate(date,type){
 if(type == 'api'){
  var oldDate = date.split("/");
  var betterDate = oldDate[2] + "-" + oldDate[0] + "-" + oldDate[1];
 
  return betterDate;
 }
 if(type == 'user'){
  var badDate = date.split("-");
  var goodDate = badDate[1] + "/" + badDate[2] + "/" + badDate[0];
  
  return goodDate;
 }
}


 

mojeebahmedmojeebahmed
Hi Sunil,
    you can also try this :

        tru.set("TB1__c", new Date("6/8/2007") );
I use this syntax to store the dates. This is valid for only US format dates. if you need to store the UK format dates, then you have to write a little function which will convert the UK format date string into a US format date string and then this line of code will work.

Let me know if you need more help.
Regards
Mojeeb
SunilSunil
Thanks. It is working now.
 
:smileyhappy: