JS string to date format Date Format String, js Date Format

Source: Internet
Author: User
Tags mscorlib

JS string to date format Date Format String, js Date Format

1/** 2 * @ author Chen weibin 3 * if you want to format the date string, you need to first convert it to the Date type Date 4 * below are several common 5*6 * var da = new Date (). format ('yyyy-MM-dd hh: mm: ss'); // convert the date format string, convert to the first format: 7 * alert ("format Date type \ n" + new Date () + "\ n is a string:" + da ); 8*9 * var str = "2014/01/01 01:01:01" // yyyy/mm/dd can be used to convert a Date to a new Date (str ); convert to the specified format 10 * alert ("Format String \ n" + str + "as Date Format \ n" + new Date (str ). format ('yyyy-MM-dd hh: mm: ss') 11*12*13 * var str1 = "00:55:55" // a string in the format of yyyy-mm-dd can be converted into a Date object using new Date (Date. parse (str. replace (/-/g, "/"); 14 * alert ("formatted string \ n" + str1 + "for Date Format \ n" + new Date (Date. parse (str1.replace (/-/g ,"/"))). format ('yyyy-MM-dd hh: mm: ss ')) 15*16*17 * Date plus month 18 * convert the character to Date type before 19 * var str1 = "00:55:55" // yyyy-mm-dd format you can use new Date (Date. parse (str. replace (/-/g, "/"); 20 * For example, var saveDate = new Date (Date. parse (str1.replace (/-/g ,"/"))). addMonth (5) 21 * addMonth (number of months) must be an integer of 22 */23 24 Date. prototype. format = function (format) {25 var date = {26 "M +": this. getMonth () + 1, 27 "d +": this. getDate (), 28 "h +": this. getHours (), 29 "m +": this. getMinutes (), 30 "s +": this. getSeconds (), 31 "q +": Math. floor (this. getMonth () + 3)/3), 32 "S +": this. getMilliseconds () 33}; 34 if (/(y +)/I. test (format) {35 format = Format. replace (RegExp. $1, (this. getFullYear () + ''). substr (4-RegExp. $1. length); 36} 37 for (var k in date) {38 if (new RegExp ("(" + k + ")"). test (format) {39 format = format. replace (RegExp. $1, RegExp. $1. length = 140? Date [k]: ("00" + date [k]). substr ("" + date [k]). length); 41} 42} 43 return format; 44} 45 Date. daysInMonth = function (year, month) {46 if (month = 1) {47 if (year % 4 = 0 & year % 100! = 0) 48 return 29; 49 else50 return 28; 51} else if (month <= 6 & month % 2 = 0) | (month = 6 & month % 2 = 1) 52 return 31; 53 else54 return 30; 55}; 56 Date. prototype. addMonth = function (addMonth) {57 var y = this. getFullYear (); 58 var m = this. getMonth (); 59 var nextY = y; 60 var nextM = m; 61 // if the current month + the month to be added> 11, here 11 is used because the js month starts from 0 62 if (m> 11) {63 nextY = y + 1; 64 nextM = parseInt (m + addMonth)-11; 65} else {66 nextM = this. getMonth () + addMonth67} 68 var daysInNextMonth = Date. daysInMonth (nextY, nextM); 69 var day = this. getDate (); 70 if (day> daysInNextMonth) {71 day = daysInNextMonth; 72} 73 return new Date (nextY, nextM, day); 74 };

 


The js function automatically formats the string date and determines whether the input date format is valid.

String in java:
What is String?
A String is a String in java. It contains several characters enclosed in double quotation marks, such as "Abc" and "one day ".
How to Create a String?
1. String s1 = "ABCD ";
2. String s2 = new String ("ABCD ");
Note:
The String class is immutable (final). Any change to the String class will return a new String class object. in this way, the reference of the String class is passed to a method. Any change of the method to the String has no effect on the object pointed to by the original reference. This is similar to the basic data type.
String s1, s2;
S1 = "abc ";
S2 = "def ";
S2 = s1;
// After this operation, s1, abc, and s2 are also abc.
String (character type)
String data type, which can contain a single character or a variable type of a string. Note that you must specify a string for the string variable in NoahWeb, and add single quotation marks (for example, 'China') at the beginning and end ').
You can use the "ADD" operator to concatenate multiple characters.
Presentation Layer example
<! -- NoahValue ValueName = "'noahweb'" -->
Sample output
NoahWeb
Example
Output a character.
Logic layer example
<SetVar Name = "actiondesc" Value = "'edit content'"/>
Example
Set the content of a local variable named actiondesc to the character "edit content"
Presentation Layer example
<! -- NoahValue ValueName = "'noahweb' add'1. 1'" -->
Sample output
NoahWeb1.1
Example
Add two strings and output them.
Logic layer example
<SetVar Name = "actiondesc" Value = "'edit content 'add' action'"/>
Example
Set the content of a local variable named actiondesc to the character "edit content action"
String type
Indicates text, that is, a series of Unicode characters.
Namespace: System
Assembly: mscorlib (in mscorlib. dll)
Syntax
Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute (True)> _
Public NotInheritable Class String
Implements IComparable, ICloneable, IConvertible, IComparable (Of String ),_
IEnumerable (Of String), IEnumerable, IEquatable (Of String)
Visual Basic (usage)
Dim ...... remaining full text>

How to convert a 201085 string to a date format of 2010 in Js

Amount
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.