Solution, understand your context and be aware of what you are doing.Īnd also be careful that omitting T or Z in a date-time stringĬan give different results in different browsers. Local/browse time zone and the UTC time zone, which is one of the mainĬomplaints with date string conversion. Won't get unwanted results because difference between your So, using in local/browser time zone you probably Gives you a date/time in local time zone as well (you only get UTC if And,īy default, almost every date method in JavaScript (except one) And when getting a date, without specifying the time zoneĪs well, the result is also converted to the browser's time zone. Specifying the time zone, JavaScript will use the local browser's time That means, when setting a date in this way, without String, the date will be local time zone instead of UTC time In ISO format, if you provide time and Z is not present in the end of Var mydate = new Date(dateString + "T00:00:00") Īnd just a few details about the solution above (but optional reading): ![]() Just concatenate your date string (using ISO format) with "T00:00:00" in the end and use the JavaScript Date() constructor, like the example below. That's the best and simpler solution in my view: Return new Date(year,month,day,hour,minute,second) Įxample: " 14:03:01".toDate("dd/mm/yyyy hh:ii:ss") Var second = secondsIndex>-1 ? dateItems : today.getSeconds() Var minute = minutesIndex>-1 ? dateItems : today.getMinutes() Var hour = hourIndex>-1 ? dateItems : today.getHours() Var day = dayIndex>-1 ? dateItems : today.getDate() Var month = monthIndex>-1 ? dateItems-1 : today.getMonth()-1 Var year = yearIndex>-1 ? dateItems : today.getFullYear() Var secondsIndex = formatItems.indexOf("ss") Var minutesIndex = formatItems.indexOf("ii") Var hourIndex = formatItems.indexOf("hh") Var yearIndex = formatItems.indexOf("yyyy") Var dayIndex = formatItems.indexOf("dd") Var monthIndex = formatItems.indexOf("mm") Var formatItems = normalizedFormat.split('-') Var normalizedFormat= format.toLowerCase().replace(//g, '-') ![]() You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.įor those who are looking for a tiny and smart solution: = function(format) Note that the number of the month must be 1 less.Īlternate method - use an appropriate library: toString().įor old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00'). To display a date in user's local time, use. ![]() To parse a date as UTC, append a Z - e.g.: new Date(''). The best practice should always be to store dates as UTC and make computations as UTC. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.Įxamples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.īut wait! Just using the "ISO format" doesn't work reliably by itself.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |