10 examples of 'javascript string to date' in JavaScript

Every line of 'javascript string to date' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
109function toDate (string) {
110 return new Date(Date.parse(string))
111}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

84function date_from_string(str) {
85 if (typeof str == 'undefined')
86 return;
87 date_time = str.split(' ');
88 date = date_time[0];
89 time = date_time[1];
90 dmy = date.split('-');
91 day = dmy[0];
92 month = dmy[1] - 1;
93 year = dmy[2];
94 hms = time.split(':');
95 hour = hms[0];
96 min = hms[1];
97 sec = hms[2];
98 return new Date(year, month, day, hour, min, sec);
99}
77function stringToDate(strDate) {
78 var dateParts = strDate.split("-");
79 return new Date(dateParts[0], (dateParts[1] - 1), dateParts[2]);
80}
12toJsDate() {
13 return new Date(this._value.getTime()); // return a clone.
14}
110function parseDate(string) {
111 var y = string.substring(0, 4);
112 var m = string.substring(4, 6);
113 var d = string.substring(6, 8);
114 return new Date(y, m, d);
115}
64function toDate (s) {
65 let a = s.split(/[^0-9]/)
66 let dateStr = a[0] + '-' + a[1] + '-' + a[2] + 'T' + a[3] + ':' + a[4] + 'Z'
67
68 return new Date(dateStr)
69}
46function parseDate(str) {
47 if (Y.Lang.isNull(str) || Y.Lang.isUndefined(str))
48 return null;
49 var parts = str.split(/[- :]/);
50 if ( parts.length < 3 )
51 return null;
52 while ( parts.length < 6 )
53 parts.push(0);
54 return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]);
55}
131function to_string_date(date) {
132 var day = date.getDate(),
133 month = date.getMonth()+1,
134 year = date.getFullYear();
135
136 day < 10 ? day = '0' + day : true;
137 month < 10 ? month = '0' + month : true;
138
139 return month+'/'+day+'/'+year;
140}
67export function date(v: Date, f?: string, lang?: string): string {
68 // TODO: localize again at some point, see https://github.com/gosquared/speed-date
69 return v.toISOString();
70}
200public toJavaScriptDate(implicitTimezone = undefined): Date {
201 const timezoneToUse =
202 this._timezone || implicitTimezone || DayTimeDuration.fromTimezoneString('Z');
203 return new Date(
204 Date.UTC(
205 this._years,
206 this._months - 1,
207 this._days,
208 this._hours - timezoneToUse.getHours(),
209 this._minutes - timezoneToUse.getMinutes(),
210 this._seconds + this.secondFraction
211 )
212 );
213}

Related snippets